Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apis/cluster/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apis/placement/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apis/placement/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 22 additions & 22 deletions pkg/controllers/clusterinventory/clusterprofile/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
clientcmdv1 "k8s.io/client-go/tools/clientcmd/api/v1"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
clusterinventory "sigs.k8s.io/cluster-inventory-api/apis/v1alpha1"
Expand Down Expand Up @@ -180,8 +181,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
return ctrl.Result{}, nil
}

// fillInClusterStatus fills in the ClusterProfile status fields from the MemberCluster status.
// Currently, it only fills in the Kubernetes version field.
// fillInClusterStatus fills in the ClusterProfile status fields from the MemberCluster status,
// including the Kubernetes version and, when available, the cluster access provider.
func (r *Reconciler) fillInClusterStatus(mc *clusterv1beta1.MemberCluster, cp *clusterinventory.ClusterProfile) {
clusterPropertyCondition := meta.FindStatusCondition(mc.Status.Conditions, string(clusterv1beta1.ConditionTypeClusterPropertyCollectionSucceeded))
if !condition.IsConditionStatusTrue(clusterPropertyCondition, mc.Generation) {
Expand All @@ -196,27 +197,26 @@ func (r *Reconciler) fillInClusterStatus(mc *clusterv1beta1.MemberCluster, cp *c
Kubernetes: k8sversion.Value,
}
}
// Add the class access provider, we only have one so far
cp.Status.AccessProviders = []clusterinventory.AccessProvider{
{
Name: controller.ClusterManagerName,
},
}
// TODO throw and unexpected error if clusterEntryPoint is not found
// We don't have a way to get it yet
clusterEntry, exists := mc.Status.Properties[propertyprovider.ClusterEntryPointProperty]
if exists {
klog.V(3).InfoS("Get Kubernetes cluster entry point from member cluster status", "clusterEntryPoint", clusterEntry.Value, "clusterProfile", klog.KObj(cp))
cp.Status.AccessProviders[0].Cluster.Server = clusterEntry.Value
}
// Get the CA Data
certificateAuthorityData, exists := mc.Status.Properties[propertyprovider.ClusterCertificateAuthorityProperty]
if exists {
klog.V(3).InfoS("Get Kubernetes cluster certificate authority data from member cluster status", "clusterProfile", klog.KObj(cp))
cp.Status.AccessProviders[0].Cluster.CertificateAuthorityData = []byte(certificateAuthorityData.Value)

// Add cluster access provider, if and only if a cluster entry point and the CA data exist as part of the
// cluster properties.
clusterEntrypoint, entryPtExists := mc.Status.Properties[propertyprovider.ClusterEntryPointProperty]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can mc.Status.Properties[propertyprovider.ClusterEntryPointProperty] ever throw nil pointer exception?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Yetkin! mc.Status is a struct (empty if not written yet, but never nil), and nil maps (.status.properties) can be read as empty maps with no panic errors, so we should be good.

caData, caDataExists := mc.Status.Properties[propertyprovider.ClusterCertificateAuthorityProperty]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question for this one too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Yetkin! mc.Status is a struct (empty if not written yet, but never nil), and nil maps (.status.properties) can be read as empty maps with no panic errors, so we should be good.

if entryPtExists && caDataExists && len(clusterEntrypoint.Value) > 0 && len(caData.Value) > 0 {
Comment thread
sjwaight marked this conversation as resolved.
cp.Status.AccessProviders = []clusterinventory.AccessProvider{
{
Name: controller.ClusterManagerName,
Cluster: clientcmdv1.Cluster{
Server: clusterEntrypoint.Value,
CertificateAuthorityData: []byte(caData.Value),
},
},
}
} else {
// throw an alert
_ = controller.NewUnexpectedBehaviorError(fmt.Errorf("cluster certificate authority data not found in member cluster %s status", mc.Name))
cp.Status.AccessProviders = nil
klog.V(2).InfoS("Cluster entry point and/or CA data is missing or empty; reset cluster access provider to cluster profile status",
"memberCluster", klog.KObj(mc), "clusterProfile", klog.KObj(cp),
"clusterEntryPointExists", entryPtExists, "caDataExists", caDataExists)
}
Comment thread
michaelawyu marked this conversation as resolved.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ var _ = Describe("Test ClusterProfile Controller", func() {
},
}
mc.Status.Properties = map[clusterv1beta1.PropertyName]clusterv1beta1.PropertyValue{
propertyprovider.ClusterEntryPointProperty: {
Value: "https://dummy-cluster-endpoint",
ObservationTime: metav1.Time{Time: time.Now()},
},
propertyprovider.ClusterCertificateAuthorityProperty: {
Value: "dummy-ca-data",
ObservationTime: metav1.Time{Time: time.Now()},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/google/go-cmp/cmp"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientcmdv1 "k8s.io/client-go/tools/clientcmd/api/v1"
clusterinventory "sigs.k8s.io/cluster-inventory-api/apis/v1alpha1"

clusterv1beta1 "github.com/kubefleet-dev/kubefleet/apis/cluster/v1beta1"
Expand Down Expand Up @@ -84,7 +85,7 @@ func TestFillInClusterStatus(t *testing.T) {
},
clusterProfile: &clusterinventory.ClusterProfile{},
expectVersion: false,
expectAccessProvider: true,
expectAccessProvider: false,
},
{
name: "Cluster property collection succeeded with k8s version only",
Expand All @@ -111,7 +112,7 @@ func TestFillInClusterStatus(t *testing.T) {
clusterProfile: &clusterinventory.ClusterProfile{},
expectVersion: true,
expectedK8sVersion: "v1.28.0",
expectAccessProvider: true,
expectAccessProvider: false,
},
{
name: "Cluster property collection succeeded with all properties",
Expand Down Expand Up @@ -176,8 +177,79 @@ func TestFillInClusterStatus(t *testing.T) {
clusterProfile: &clusterinventory.ClusterProfile{},
expectVersion: true,
expectedK8sVersion: "v1.27.5",
expectAccessProvider: true,
expectedServer: "https://api.partial-cluster.example.com:6443",
expectAccessProvider: false,
},
{
name: "Cluster property collection succeeded but access provider properties are empty",
memberCluster: &clusterv1beta1.MemberCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
Generation: 1,
},
Status: clusterv1beta1.MemberClusterStatus{
Conditions: []metav1.Condition{
{
Type: string(clusterv1beta1.ConditionTypeClusterPropertyCollectionSucceeded),
Status: metav1.ConditionTrue,
ObservedGeneration: 1,
},
},
Properties: map[clusterv1beta1.PropertyName]clusterv1beta1.PropertyValue{
propertyprovider.K8sVersionProperty: {
Value: "v1.30.0",
},
propertyprovider.ClusterEntryPointProperty: {
Value: "",
},
propertyprovider.ClusterCertificateAuthorityProperty: {
Value: "",
},
},
},
},
clusterProfile: &clusterinventory.ClusterProfile{},
expectVersion: true,
expectedK8sVersion: "v1.30.0",
expectAccessProvider: false,
},
{
name: "Access provider properties missing resets a previously populated access provider",
memberCluster: &clusterv1beta1.MemberCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
Generation: 1,
},
Status: clusterv1beta1.MemberClusterStatus{
Conditions: []metav1.Condition{
{
Type: string(clusterv1beta1.ConditionTypeClusterPropertyCollectionSucceeded),
Status: metav1.ConditionTrue,
ObservedGeneration: 1,
},
},
Properties: map[clusterv1beta1.PropertyName]clusterv1beta1.PropertyValue{
propertyprovider.K8sVersionProperty: {
Value: "v1.31.0",
},
},
},
},
clusterProfile: &clusterinventory.ClusterProfile{
Status: clusterinventory.ClusterProfileStatus{
AccessProviders: []clusterinventory.AccessProvider{
{
Name: controller.ClusterManagerName,
Cluster: clientcmdv1.Cluster{
Server: "https://api.stale-cluster.example.com:6443",
CertificateAuthorityData: []byte("c3RhbGUtY2EtZGF0YQ=="),
},
},
},
},
},
expectVersion: true,
expectedK8sVersion: "v1.31.0",
expectAccessProvider: false,
},
}

Expand Down
2 changes: 1 addition & 1 deletion test/apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading