From 2beb284d52b96d6bdad66cc5270646b249406682 Mon Sep 17 00:00:00 2001 From: Fabian Fischer Date: Wed, 18 Aug 2021 13:25:05 +0200 Subject: [PATCH 1/3] Add dynamic fact field to CRD --- api/v1alpha1/cluster_types.go | 2 ++ api/v1alpha1/zz_generated.deepcopy.go | 7 +++++++ config/crd/bases/syn.tools_clusters.yaml | 5 +++++ 3 files changed, 14 insertions(+) diff --git a/api/v1alpha1/cluster_types.go b/api/v1alpha1/cluster_types.go index cba2ba52..8db953e3 100644 --- a/api/v1alpha1/cluster_types.go +++ b/api/v1alpha1/cluster_types.go @@ -51,6 +51,8 @@ type BootstrapToken struct { type ClusterStatus struct { // BootstrapTokenValid validity of the bootstrap token, set by the Lieutenant API. BootstrapToken *BootstrapToken `json:"bootstrapToken,omitempty"` + // Facts are key/value pairs for dynamically fetched facts + Facts Facts `json:"facts,omitempty"` } // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 9ddfcdda..ef61d256 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -119,6 +119,13 @@ func (in *ClusterStatus) DeepCopyInto(out *ClusterStatus) { *out = new(BootstrapToken) (*in).DeepCopyInto(*out) } + if in.Facts != nil { + in, out := &in.Facts, &out.Facts + *out = make(Facts, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterStatus. diff --git a/config/crd/bases/syn.tools_clusters.yaml b/config/crd/bases/syn.tools_clusters.yaml index 05c0b39d..9b8d5fe7 100644 --- a/config/crd/bases/syn.tools_clusters.yaml +++ b/config/crd/bases/syn.tools_clusters.yaml @@ -185,6 +185,11 @@ spec: format: date-time type: string type: object + facts: + additionalProperties: + type: string + description: Facts are key/value pairs for dynamically fetched facts + type: object type: object type: object served: true From 5d3a73a0fec0823c90a41327f559cb63e493cd72 Mon Sep 17 00:00:00 2001 From: Fabian Fischer Date: Wed, 18 Aug 2021 17:49:56 +0200 Subject: [PATCH 2/3] Update role to allow steward to update the status --- controllers/cluster/rbac.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/controllers/cluster/rbac.go b/controllers/cluster/rbac.go index b176913f..dae41867 100644 --- a/controllers/cluster/rbac.go +++ b/controllers/cluster/rbac.go @@ -23,7 +23,7 @@ func createClusterRBAC(obj pipeline.Object, data *pipeline.Context) pipeline.Res ObjectMeta: objMeta, Rules: []rbacv1.PolicyRule{{ APIGroups: []string{synv1alpha1.GroupVersion.Group}, - Resources: []string{"clusters"}, + Resources: []string{"clusters", "clusters/status"}, Verbs: []string{"get", "update"}, ResourceNames: []string{obj.GetName()}, }}, @@ -41,7 +41,16 @@ func createClusterRBAC(obj pipeline.Object, data *pipeline.Context) pipeline.Res }}, } for _, item := range []client.Object{serviceAccount, role, roleBinding} { - if err := data.Client.Create(context.TODO(), item); err != nil && !errors.IsAlreadyExists(err) { + found := item.DeepCopyObject().(client.Object) + err := data.Client.Get(context.TODO(), client.ObjectKeyFromObject(item), found) + if errors.IsNotFound(err) { + if err := data.Client.Create(context.TODO(), item); err != nil && !errors.IsAlreadyExists(err) { + return pipeline.Result{Err: err} + } + } else if err != nil { + return pipeline.Result{Err: err} + } + if err := data.Client.Update(context.TODO(), item); err != nil { return pipeline.Result{Err: err} } } From fb96c44b1c43827993ab81ddee5cdfb5255995fe Mon Sep 17 00:00:00 2001 From: Fabian Fischer Date: Thu, 19 Aug 2021 10:34:13 +0200 Subject: [PATCH 3/3] Restructure code flow after codereview input --- controllers/cluster/rbac.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/controllers/cluster/rbac.go b/controllers/cluster/rbac.go index dae41867..265d582e 100644 --- a/controllers/cluster/rbac.go +++ b/controllers/cluster/rbac.go @@ -43,11 +43,12 @@ func createClusterRBAC(obj pipeline.Object, data *pipeline.Context) pipeline.Res for _, item := range []client.Object{serviceAccount, role, roleBinding} { found := item.DeepCopyObject().(client.Object) err := data.Client.Get(context.TODO(), client.ObjectKeyFromObject(item), found) - if errors.IsNotFound(err) { - if err := data.Client.Create(context.TODO(), item); err != nil && !errors.IsAlreadyExists(err) { - return pipeline.Result{Err: err} + if err != nil { + if errors.IsNotFound(err) { + if err := data.Client.Create(context.TODO(), item); err != nil && !errors.IsAlreadyExists(err) { + return pipeline.Result{Err: err} + } } - } else if err != nil { return pipeline.Result{Err: err} } if err := data.Client.Update(context.TODO(), item); err != nil {