From 77c0579e23a54927ba3c759b4d83746d01d77629 Mon Sep 17 00:00:00 2001 From: EricoGR Date: Sat, 20 Mar 2021 16:00:27 -0300 Subject: [PATCH] - simplify create/update object - improve log - fix bug with object copy - add more tests - rename folders - remove multigroup --- Makefile | 4 +- PROJECT | 7 +- README.md | 14 +- .../v1/groupversion_info.go | 6 +- .../v1/objecttemplate_types.go | 0 .../v1/objecttemplateparams_types.go | 0 .../v1/objecttemplateparams_types_util.go | 0 .../v1/zz_generated.deepcopy.go | 0 ....ericogr.com.br_objecttemplateparams.yaml} | 4 +- ...e.k8s.ericogr.com.br_objecttemplates.yaml} | 4 +- config/crd/kustomization.yaml | 4 +- .../cainjection_in_objecttemplateparams.yaml | 2 +- .../cainjection_in_objecttemplates.yaml | 2 +- .../webhook_in_objecttemplateparams.yaml | 2 +- .../patches/webhook_in_objecttemplates.yaml | 2 +- config/rbac/objecttemplate_editor_role.yaml | 4 +- config/rbac/objecttemplate_viewer_role.yaml | 4 +- .../objecttemplateparams_editor_role.yaml | 4 +- .../objecttemplateparams_viewer_role.yaml | 4 +- config/rbac/role.yaml | 8 +- ...k8s.ericogr.com.br_v1_objecttemplate.yaml} | 2 +- ...icogr.com.br_v1_objecttemplateparams.yaml} | 3 +- .../commons.go | 61 +++---- .../commons_test.go | 2 +- .../controller_log.go | 0 .../controller_map_test.go} | 23 +-- controllers/template/controller_pod_test.go | 163 ++++++++++++++++++ .../object_utilities.go | 0 .../objecttemplate_controller.go | 6 +- .../objecttemplateparams_controller.go | 6 +- .../suite_test.go | 2 +- main.go | 9 +- specs/object-template-operator.yaml | 16 +- 33 files changed, 253 insertions(+), 115 deletions(-) rename apis/{template.ericogr.github.com => }/v1/groupversion_info.go (81%) rename apis/{template.ericogr.github.com => }/v1/objecttemplate_types.go (100%) rename apis/{template.ericogr.github.com => }/v1/objecttemplateparams_types.go (100%) rename apis/{template.ericogr.github.com => }/v1/objecttemplateparams_types_util.go (100%) rename apis/{template.ericogr.github.com => }/v1/zz_generated.deepcopy.go (100%) rename config/crd/bases/{template.ericogr.github.com_objecttemplateparams.yaml => template.k8s.ericogr.com.br_objecttemplateparams.yaml} (96%) rename config/crd/bases/{template.ericogr.github.com_objecttemplates.yaml => template.k8s.ericogr.com.br_objecttemplates.yaml} (97%) rename config/samples/{template.ericogr.github.com_v1_objecttemplate.yaml => template.k8s.ericogr.com.br_v1_objecttemplate.yaml} (90%) rename config/samples/{template.ericogr.github.com_v1_objecttemplateparams.yaml => template.k8s.ericogr.com.br_v1_objecttemplateparams.yaml} (73%) rename controllers/{template.ericogr.github.com => template}/commons.go (82%) rename controllers/{template.ericogr.github.com => template}/commons_test.go (92%) rename controllers/{template.ericogr.github.com => template}/controller_log.go (100%) rename controllers/{template.ericogr.github.com/objecttemplateparams_controller_test.go => template/controller_map_test.go} (92%) create mode 100644 controllers/template/controller_pod_test.go rename controllers/{template.ericogr.github.com => template}/object_utilities.go (100%) rename controllers/{template.ericogr.github.com => template}/objecttemplate_controller.go (93%) rename controllers/{template.ericogr.github.com => template}/objecttemplateparams_controller.go (93%) rename controllers/{template.ericogr.github.com => template}/suite_test.go (97%) diff --git a/Makefile b/Makefile index ffb04bf..1c52473 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ all: manager # Run tests test: generate fmt vet manifests - go test ./... -coverprofile cover.out + go test -v ./... -coverprofile cover.out # Build manager binary manager: generate fmt vet @@ -68,7 +68,7 @@ docker-push: docker push ${IMG} security: gosec - gosec . controllers/template.ericogr.github.com apis/template.ericogr.github.com/v1/ + gosec . controllers/template.k8s.ericogr.com.br apis/template.k8s.ericogr.com.br/v1/ # find or download controller-gen # download controller-gen if necessary diff --git a/PROJECT b/PROJECT index c22a1bd..ec9655a 100644 --- a/PROJECT +++ b/PROJECT @@ -1,11 +1,10 @@ -domain: github.com -multigroup: true +domain: k8s.ericogr.com.br repo: github.com/ericogr/k8s-object-template resources: -- group: template.ericogr.github.com +- group: template kind: ObjectTemplate version: v1 -- group: template.ericogr.github.com +- group: template kind: ObjectTemplateParams version: v1 version: "2" diff --git a/README.md b/README.md index fbc64c0..7d67e04 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ rules: - update # << - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplateparams verbs: @@ -51,7 +51,7 @@ rules: - update - watch - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplateparams/status verbs: @@ -59,7 +59,7 @@ rules: - patch - update - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplates verbs: @@ -71,7 +71,7 @@ rules: - update - watch - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplates/status verbs: @@ -80,7 +80,7 @@ rules: - update ``` # New Custom Resource Definitions (CRD's) -You have two new CRD's: [ObjectTemplate](config/crd/bases/template.ericogr.github.com_objecttemplates.yaml) and [ObjectTemplateParameters](config/crd/bases/template.ericogr.github.com_objecttemplateparams.yaml). +You have two new CRD's: [ObjectTemplate](config/crd/bases/template.k8s.ericogr.com.br_objecttemplates.yaml) and [ObjectTemplateParameters](config/crd/bases/template.k8s.ericogr.com.br_objecttemplateparams.yaml). **ObjectTemplate (cluster scope):** template used to create kubernetes objects at users namespaces (can be used by k8s admins) @@ -93,7 +93,7 @@ Use templates as a base to create kubernetes objects. Users can define your own ```yaml --- -apiVersion: template.ericogr.github.com/v1 +apiVersion: template.k8s.ericogr.com.br/v1 kind: ObjectTemplate metadata: name: objecttemplate-configmap-test @@ -157,7 +157,7 @@ Users can define your own parameters to create new objects based on templates in ```yaml --- -apiVersion: template.ericogr.github.com/v1 +apiVersion: template.k8s.ericogr.com.br/v1 kind: ObjectTemplateParams metadata: name: objecttemplateparams-sample diff --git a/apis/template.ericogr.github.com/v1/groupversion_info.go b/apis/v1/groupversion_info.go similarity index 81% rename from apis/template.ericogr.github.com/v1/groupversion_info.go rename to apis/v1/groupversion_info.go index 4f1892a..bd264cf 100644 --- a/apis/template.ericogr.github.com/v1/groupversion_info.go +++ b/apis/v1/groupversion_info.go @@ -14,9 +14,9 @@ See the License for the specific language governing permissions and limitations under the License. */ -// Package v1 contains API Schema definitions for the template.ericogr.github.com v1 API group +// Package v1 contains API Schema definitions for the template.k8s.ericogr.com.br v1 API group // +kubebuilder:object:generate=true -// +groupName=template.ericogr.github.com +// +groupName=template.k8s.ericogr.com.br package v1 import ( @@ -26,7 +26,7 @@ import ( var ( // GroupVersion is group version used to register these objects - GroupVersion = schema.GroupVersion{Group: "template.ericogr.github.com", Version: "v1"} + GroupVersion = schema.GroupVersion{Group: "template.k8s.ericogr.com.br", Version: "v1"} // SchemeBuilder is used to add go types to the GroupVersionKind scheme SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion} diff --git a/apis/template.ericogr.github.com/v1/objecttemplate_types.go b/apis/v1/objecttemplate_types.go similarity index 100% rename from apis/template.ericogr.github.com/v1/objecttemplate_types.go rename to apis/v1/objecttemplate_types.go diff --git a/apis/template.ericogr.github.com/v1/objecttemplateparams_types.go b/apis/v1/objecttemplateparams_types.go similarity index 100% rename from apis/template.ericogr.github.com/v1/objecttemplateparams_types.go rename to apis/v1/objecttemplateparams_types.go diff --git a/apis/template.ericogr.github.com/v1/objecttemplateparams_types_util.go b/apis/v1/objecttemplateparams_types_util.go similarity index 100% rename from apis/template.ericogr.github.com/v1/objecttemplateparams_types_util.go rename to apis/v1/objecttemplateparams_types_util.go diff --git a/apis/template.ericogr.github.com/v1/zz_generated.deepcopy.go b/apis/v1/zz_generated.deepcopy.go similarity index 100% rename from apis/template.ericogr.github.com/v1/zz_generated.deepcopy.go rename to apis/v1/zz_generated.deepcopy.go diff --git a/config/crd/bases/template.ericogr.github.com_objecttemplateparams.yaml b/config/crd/bases/template.k8s.ericogr.com.br_objecttemplateparams.yaml similarity index 96% rename from config/crd/bases/template.ericogr.github.com_objecttemplateparams.yaml rename to config/crd/bases/template.k8s.ericogr.com.br_objecttemplateparams.yaml index 33eaeb3..1aeb975 100644 --- a/config/crd/bases/template.ericogr.github.com_objecttemplateparams.yaml +++ b/config/crd/bases/template.k8s.ericogr.com.br_objecttemplateparams.yaml @@ -6,7 +6,7 @@ metadata: annotations: controller-gen.kubebuilder.io/version: v0.2.5 creationTimestamp: null - name: objecttemplateparams.template.ericogr.github.com + name: objecttemplateparams.template.k8s.ericogr.com.br spec: additionalPrinterColumns: - JSONPath: .status.status @@ -15,7 +15,7 @@ spec: - JSONPath: .metadata.creationTimestamp name: age type: date - group: template.ericogr.github.com + group: template.k8s.ericogr.com.br names: kind: ObjectTemplateParams listKind: ObjectTemplateParamsList diff --git a/config/crd/bases/template.ericogr.github.com_objecttemplates.yaml b/config/crd/bases/template.k8s.ericogr.com.br_objecttemplates.yaml similarity index 97% rename from config/crd/bases/template.ericogr.github.com_objecttemplates.yaml rename to config/crd/bases/template.k8s.ericogr.com.br_objecttemplates.yaml index b150827..d3a4c7a 100644 --- a/config/crd/bases/template.ericogr.github.com_objecttemplates.yaml +++ b/config/crd/bases/template.k8s.ericogr.com.br_objecttemplates.yaml @@ -6,7 +6,7 @@ metadata: annotations: controller-gen.kubebuilder.io/version: v0.2.5 creationTimestamp: null - name: objecttemplates.template.ericogr.github.com + name: objecttemplates.template.k8s.ericogr.com.br spec: additionalPrinterColumns: - JSONPath: .status.status @@ -15,7 +15,7 @@ spec: - JSONPath: .metadata.creationTimestamp name: age type: date - group: template.ericogr.github.com + group: template.k8s.ericogr.com.br names: kind: ObjectTemplate listKind: ObjectTemplateList diff --git a/config/crd/kustomization.yaml b/config/crd/kustomization.yaml index f1162d4..9807448 100644 --- a/config/crd/kustomization.yaml +++ b/config/crd/kustomization.yaml @@ -2,8 +2,8 @@ # since it depends on service name and namespace that are out of this kustomize package. # It should be run by config/default resources: -- bases/template.ericogr.github.com_objecttemplates.yaml -- bases/template.ericogr.github.com_objecttemplateparams.yaml +- bases/template.k8s.ericogr.com.br_objecttemplates.yaml +- bases/template.k8s.ericogr.com.br_objecttemplateparams.yaml # +kubebuilder:scaffold:crdkustomizeresource patchesStrategicMerge: diff --git a/config/crd/patches/cainjection_in_objecttemplateparams.yaml b/config/crd/patches/cainjection_in_objecttemplateparams.yaml index 0b5e20c..633cddf 100644 --- a/config/crd/patches/cainjection_in_objecttemplateparams.yaml +++ b/config/crd/patches/cainjection_in_objecttemplateparams.yaml @@ -5,4 +5,4 @@ kind: CustomResourceDefinition metadata: annotations: cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME) - name: objecttemplateparams.template.ericogr.github.com + name: objecttemplateparams.template.k8s.ericogr.com.br diff --git a/config/crd/patches/cainjection_in_objecttemplates.yaml b/config/crd/patches/cainjection_in_objecttemplates.yaml index f21e1c2..42ac01d 100644 --- a/config/crd/patches/cainjection_in_objecttemplates.yaml +++ b/config/crd/patches/cainjection_in_objecttemplates.yaml @@ -5,4 +5,4 @@ kind: CustomResourceDefinition metadata: annotations: cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME) - name: objecttemplates.template.ericogr.github.com + name: objecttemplates.template.k8s.ericogr.com.br diff --git a/config/crd/patches/webhook_in_objecttemplateparams.yaml b/config/crd/patches/webhook_in_objecttemplateparams.yaml index bb7bcf1..2dd3ee4 100644 --- a/config/crd/patches/webhook_in_objecttemplateparams.yaml +++ b/config/crd/patches/webhook_in_objecttemplateparams.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1beta1 kind: CustomResourceDefinition metadata: - name: objecttemplateparams.template.ericogr.github.com + name: objecttemplateparams.template.k8s.ericogr.com.br spec: conversion: strategy: Webhook diff --git a/config/crd/patches/webhook_in_objecttemplates.yaml b/config/crd/patches/webhook_in_objecttemplates.yaml index 6d41c9d..de8df45 100644 --- a/config/crd/patches/webhook_in_objecttemplates.yaml +++ b/config/crd/patches/webhook_in_objecttemplates.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1beta1 kind: CustomResourceDefinition metadata: - name: objecttemplates.template.ericogr.github.com + name: objecttemplates.template.k8s.ericogr.com.br spec: conversion: strategy: Webhook diff --git a/config/rbac/objecttemplate_editor_role.yaml b/config/rbac/objecttemplate_editor_role.yaml index 743818f..123d15e 100644 --- a/config/rbac/objecttemplate_editor_role.yaml +++ b/config/rbac/objecttemplate_editor_role.yaml @@ -5,7 +5,7 @@ metadata: name: objecttemplate-editor-role rules: - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplates verbs: @@ -17,7 +17,7 @@ rules: - update - watch - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplates/status verbs: diff --git a/config/rbac/objecttemplate_viewer_role.yaml b/config/rbac/objecttemplate_viewer_role.yaml index 441ce3c..a3bb31d 100644 --- a/config/rbac/objecttemplate_viewer_role.yaml +++ b/config/rbac/objecttemplate_viewer_role.yaml @@ -5,7 +5,7 @@ metadata: name: objecttemplate-viewer-role rules: - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplates verbs: @@ -13,7 +13,7 @@ rules: - list - watch - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplates/status verbs: diff --git a/config/rbac/objecttemplateparams_editor_role.yaml b/config/rbac/objecttemplateparams_editor_role.yaml index e00c8d9..856d966 100644 --- a/config/rbac/objecttemplateparams_editor_role.yaml +++ b/config/rbac/objecttemplateparams_editor_role.yaml @@ -5,7 +5,7 @@ metadata: name: objecttemplateparams-editor-role rules: - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplateparams verbs: @@ -17,7 +17,7 @@ rules: - update - watch - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplateparams/status verbs: diff --git a/config/rbac/objecttemplateparams_viewer_role.yaml b/config/rbac/objecttemplateparams_viewer_role.yaml index e416a22..359da77 100644 --- a/config/rbac/objecttemplateparams_viewer_role.yaml +++ b/config/rbac/objecttemplateparams_viewer_role.yaml @@ -5,7 +5,7 @@ metadata: name: objecttemplateparams-viewer-role rules: - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplateparams verbs: @@ -13,7 +13,7 @@ rules: - list - watch - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplateparams/status verbs: diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index 4903aca..a30a663 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -18,7 +18,7 @@ rules: - update - watch - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplateparams verbs: @@ -30,7 +30,7 @@ rules: - update - watch - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplateparams/status verbs: @@ -38,7 +38,7 @@ rules: - patch - update - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplates verbs: @@ -50,7 +50,7 @@ rules: - update - watch - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplates/status verbs: diff --git a/config/samples/template.ericogr.github.com_v1_objecttemplate.yaml b/config/samples/template.k8s.ericogr.com.br_v1_objecttemplate.yaml similarity index 90% rename from config/samples/template.ericogr.github.com_v1_objecttemplate.yaml rename to config/samples/template.k8s.ericogr.com.br_v1_objecttemplate.yaml index c25cdfb..c98c9f1 100644 --- a/config/samples/template.ericogr.github.com_v1_objecttemplate.yaml +++ b/config/samples/template.k8s.ericogr.com.br_v1_objecttemplate.yaml @@ -1,5 +1,5 @@ --- -apiVersion: template.ericogr.github.com/v1 +apiVersion: template.k8s.ericogr.com.br/v1 kind: ObjectTemplate metadata: name: objecttemplate-configmap-test diff --git a/config/samples/template.ericogr.github.com_v1_objecttemplateparams.yaml b/config/samples/template.k8s.ericogr.com.br_v1_objecttemplateparams.yaml similarity index 73% rename from config/samples/template.ericogr.github.com_v1_objecttemplateparams.yaml rename to config/samples/template.k8s.ericogr.com.br_v1_objecttemplateparams.yaml index 67887de..68b2ba9 100644 --- a/config/samples/template.ericogr.github.com_v1_objecttemplateparams.yaml +++ b/config/samples/template.k8s.ericogr.com.br_v1_objecttemplateparams.yaml @@ -1,9 +1,8 @@ --- -apiVersion: template.ericogr.github.com/v1 +apiVersion: template.k8s.ericogr.com.br/v1 kind: ObjectTemplateParams metadata: name: objecttemplateparams-sample - namespace: default spec: templates: - name: objecttemplate-configmap-test diff --git a/controllers/template.ericogr.github.com/commons.go b/controllers/template/commons.go similarity index 82% rename from controllers/template.ericogr.github.com/commons.go rename to controllers/template/commons.go index 264d52c..9493b03 100644 --- a/controllers/template.ericogr.github.com/commons.go +++ b/controllers/template/commons.go @@ -4,10 +4,9 @@ import ( "context" "fmt" - otv1 "github.com/ericogr/k8s-object-template/apis/template.ericogr.github.com/v1" + otv1 "github.com/ericogr/k8s-object-template/apis/v1" "github.com/go-logr/logr" corev1 "k8s.io/api/core/v1" - k8sErrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" @@ -15,6 +14,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/serializer/yaml" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" ) var ( @@ -54,44 +54,31 @@ func (c *Common) UpdateSingleObjectByTemplate(obj otv1.Object, owners []metav1.O } log.Info(fmt.Sprintf("Object encoded succefully %v", reference)) - findObj, err := c.GetObject( - *gvk, - types.NamespacedName{ - Namespace: namespaceName, - Name: obj.Name, - }, - ) - - // controllerutil.CreateOrUpdate(ctx, c.Client, &newObj, func() error { - // return nil - // }) - - if err != nil && k8sErrors.IsNotFound(err) { - log.Info(fmt.Sprintf("Creating new object %v", reference)) - err := c.Client.Create(ctx, &newObj) - - if err == nil { - log.Info(fmt.Sprintf("Create succefully %v", reference)) + findObj := unstructured.Unstructured{} + findObj.SetName(obj.Name) + findObj.SetNamespace(namespaceName) + findObj.SetGroupVersionKind(*gvk) + + res, err := controllerutil.CreateOrUpdate(ctx, c.Client, &findObj, func() error { + findObj.Object["data"] = newObj.Object["data"] + findObj.Object["spec"] = newObj.Object["spec"] + findObj.SetLabels(newObj.GetLabels()) + findObj.SetAnnotations(newObj.GetAnnotations()) + return nil + }) + + if err == nil { + if res == controllerutil.OperationResultCreated { + log.Info(fmt.Sprintf("Created succefully %v", reference)) + } else if res == controllerutil.OperationResultUpdated { + log.Info(fmt.Sprintf("Update succefully %v", reference)) + } else if res == controllerutil.OperationResultNone { + log.Info(fmt.Sprintf("Not updated nor created %v", reference)) } else { - return fmt.Errorf("Error creating object %v: %v", reference, err.Error()) + log.Info(fmt.Sprintf("Unknown status %v for %v", res, reference)) } } else { - if err == nil { - originalResourceVersion := findObj.GetResourceVersion() - findObj.Object = newObj.Object - findObj.SetResourceVersion(originalResourceVersion) - findObj.SetLabels(newObj.GetLabels()) - findObj.SetAnnotations(newObj.GetAnnotations()) - err := c.Client.Update(ctx, &findObj) - - if err == nil { - log.Info(fmt.Sprintf("Update succefully %v", reference)) - } else { - return fmt.Errorf("Error updating object %v: %v", reference, err.Error()) - } - } else { - return fmt.Errorf("Error getting object %v: %v", reference, err.Error()) - } + return fmt.Errorf("Error updating object %v: %v", reference, err.Error()) } return nil diff --git a/controllers/template.ericogr.github.com/commons_test.go b/controllers/template/commons_test.go similarity index 92% rename from controllers/template.ericogr.github.com/commons_test.go rename to controllers/template/commons_test.go index dcaa8bb..3c55f64 100644 --- a/controllers/template.ericogr.github.com/commons_test.go +++ b/controllers/template/commons_test.go @@ -17,7 +17,7 @@ limitations under the License. package controllers import ( - otv1 "github.com/ericogr/k8s-object-template/apis/template.ericogr.github.com/v1" + otv1 "github.com/ericogr/k8s-object-template/apis/v1" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) diff --git a/controllers/template.ericogr.github.com/controller_log.go b/controllers/template/controller_log.go similarity index 100% rename from controllers/template.ericogr.github.com/controller_log.go rename to controllers/template/controller_log.go diff --git a/controllers/template.ericogr.github.com/objecttemplateparams_controller_test.go b/controllers/template/controller_map_test.go similarity index 92% rename from controllers/template.ericogr.github.com/objecttemplateparams_controller_test.go rename to controllers/template/controller_map_test.go index 5b08999..11b6d46 100644 --- a/controllers/template.ericogr.github.com/objecttemplateparams_controller_test.go +++ b/controllers/template/controller_map_test.go @@ -26,10 +26,10 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" - otv1 "github.com/ericogr/k8s-object-template/apis/template.ericogr.github.com/v1" + otv1 "github.com/ericogr/k8s-object-template/apis/v1" ) -var _ = Describe("ObjectTemplateParams controller", func() { +var _ = Describe("ObjectTemplateParams controller (ConfigMap)", func() { const ( ObjectTemplateParamsNamespace = "default" ObjectTemplateParamsName = "otp-name" @@ -40,11 +40,11 @@ var _ = Describe("ObjectTemplateParams controller", func() { ) Context("When updating parameters from ObjectTemplateParams", func() { It("Should update templated object.", func() { - By("By creating a new ObjectTemplate") + By("By creating a new ObjectTemplate as a kubernetes map") ctx := context.Background() objectTemplate := &otv1.ObjectTemplate{ TypeMeta: metav1.TypeMeta{ - APIVersion: "template.ericogr.github.com/v1", + APIVersion: "template.k8s.ericogr.com.br/v1", Kind: "ObjectTemplate", }, ObjectMeta: metav1.ObjectMeta{ @@ -79,18 +79,14 @@ var _ = Describe("ObjectTemplateParams controller", func() { createdObjectTemplate := &otv1.ObjectTemplate{} Eventually(func() bool { err := k8sClient.Get(ctx, types.NamespacedName{Name: ObjectTemplateName}, createdObjectTemplate) - - if err != nil { - return false - } - return true + return err == nil }, timeout, interval).Should(BeTrue()) Expect(createdObjectTemplate.Spec.Objects).Should(HaveLen(1)) By("Creating a new ObjectTemplateParam") objectTemplateParams := &otv1.ObjectTemplateParams{ TypeMeta: metav1.TypeMeta{ - APIVersion: "template.ericogr.github.com/v1", + APIVersion: "template.k8s.ericogr.com.br/v1", Kind: "ObjectTemplateParam", }, ObjectMeta: metav1.ObjectMeta{ @@ -118,11 +114,7 @@ var _ = Describe("ObjectTemplateParams controller", func() { ctx, types.NamespacedName{Name: NewObjectName, Namespace: ObjectTemplateParamsNamespace}, &configmap) - - if err != nil { - return false - } - return true + return err == nil }, timeout, interval).Should(BeTrue()) Expect(configmap.Data["player_initial_lives"]).Should(BeIdenticalTo("3")) Expect(configmap.Data["ui_properties_file_name"]).Should(BeIdenticalTo("user-interface.properties")) @@ -142,7 +134,6 @@ var _ = Describe("ObjectTemplateParams controller", func() { new_player_initial_lives: "{{ .lives }}" new_ui_properties_file_name: "{{ .properties_file }}"` Expect(k8sClient.Update(ctx, createdObjectTemplate)).Should(Succeed()) - Eventually(func() bool { err := k8sClient.Get(ctx, types.NamespacedName{Name: NewObjectName, Namespace: ObjectTemplateParamsNamespace}, &configmap) diff --git a/controllers/template/controller_pod_test.go b/controllers/template/controller_pod_test.go new file mode 100644 index 0000000..ab0d441 --- /dev/null +++ b/controllers/template/controller_pod_test.go @@ -0,0 +1,163 @@ +/* + + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package controllers + +import ( + "context" + "time" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + + otv1 "github.com/ericogr/k8s-object-template/apis/v1" +) + +var _ = Describe("ObjectTemplateParams controller (POD)", func() { + const ( + ObjectTemplateParamsNamespace = "default" + ObjectTemplateParamsName = "otp-pod-name" + ObjectTemplateName = "ot-pod-name" + NewObjectName = "new-pod-name" + timeout = time.Second * 5 + interval = time.Second * 1 + ) + Context("When updating parameters from ObjectTemplateParams", func() { + It("Should update templated object.", func() { + By("By creating a new ObjectTemplate as a kubernetes map") + ctx := context.Background() + objectTemplate := &otv1.ObjectTemplate{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "template.k8s.ericogr.com.br/v1", + Kind: "ObjectTemplate", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: ObjectTemplateName, + }, + Spec: otv1.ObjectTemplateSpec{ + Description: "namespace-template", + Objects: []otv1.Object{ + { + Kind: "Pod", + Metadata: otv1.Metadata{ + Annotations: map[string]string{ + "annotation1": "value_annotation1", + "annotation2": "value_annotation2", + }, + Labels: map[string]string{ + "label1": "value_label1", + "label2": "value_label2", + }, + }, + APIVersion: "v1", + Name: NewObjectName, + TemplateBody: `spec: + containers: + - name: mynginx + image: {{ .imageName }} + activeDeadlineSeconds: 60 +`, + }, + }, + }, + } + Expect(k8sClient.Create(ctx, objectTemplate)).Should(Succeed()) + + createdObjectTemplate := &otv1.ObjectTemplate{} + Eventually(func() bool { + err := k8sClient.Get(ctx, types.NamespacedName{Name: ObjectTemplateName}, createdObjectTemplate) + return err == nil + }, timeout, interval).Should(BeTrue()) + Expect(createdObjectTemplate.Spec.Objects).Should(HaveLen(1)) + + By("Creating a new ObjectTemplateParam") + objectTemplateParams := &otv1.ObjectTemplateParams{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "template.k8s.ericogr.com.br/v1", + Kind: "ObjectTemplateParam", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: ObjectTemplateParamsName, + Namespace: ObjectTemplateParamsNamespace, + }, + Spec: otv1.ObjectTemplateParamsSpec{ + Templates: []otv1.Parameters{ + { + Name: ObjectTemplateName, + Values: map[string]string{ + "imageName": "nginx", + }, + }, + }, + }, + } + Expect(k8sClient.Create(ctx, objectTemplateParams)).Should(Succeed()) + + By("By checking object in template was created") + var pod corev1.Pod + Eventually(func() bool { + err := k8sClient.Get( + ctx, + types.NamespacedName{Name: NewObjectName, Namespace: ObjectTemplateParamsNamespace}, + &pod) + return err == nil + }, timeout, interval).Should(BeTrue()) + + Expect(pod.Spec.Containers[0].Name).Should(BeIdenticalTo("mynginx")) + Expect(pod.Spec.Containers[0].Image).Should(BeIdenticalTo("nginx")) + Expect(*pod.Spec.ActiveDeadlineSeconds == 60).Should(BeTrue()) + Expect(pod.ObjectMeta.Annotations["annotation1"]).Should(BeIdenticalTo("value_annotation1")) + Expect(pod.ObjectMeta.Annotations["annotation2"]).Should(BeIdenticalTo("value_annotation2")) + Expect(pod.ObjectMeta.Labels["label1"]).Should(BeIdenticalTo("value_label1")) + Expect(pod.ObjectMeta.Labels["label2"]).Should(BeIdenticalTo("value_label2")) + + By("By updating object template") + Expect(k8sClient.Get(ctx, types.NamespacedName{Name: ObjectTemplateName}, createdObjectTemplate)).Should(Succeed()) + + createdObjectTemplate.Spec.Objects[0].Metadata.Annotations["annotation2"] = "value_annotation2_new" + createdObjectTemplate.Spec.Objects[0].Metadata.Annotations["annotation3"] = "value_annotation3" + createdObjectTemplate.Spec.Objects[0].Metadata.Labels["label1"] = "value_label1_new" + createdObjectTemplate.Spec.Objects[0].Metadata.Labels["label3"] = "value_label3" + createdObjectTemplate.Spec.Objects[0].TemplateBody = `spec: + containers: + - name: mynginx + image: {{ .imageName }} + activeDeadlineSeconds: 30 +` + Expect(k8sClient.Update(ctx, createdObjectTemplate)).Should(Succeed()) + Eventually(func() bool { + err := k8sClient.Get(ctx, types.NamespacedName{Name: NewObjectName, Namespace: ObjectTemplateParamsNamespace}, &pod) + + if err != nil { + return false + } + + return pod.Spec.Containers[0].Name == "mynginx" && + pod.Spec.Containers[0].Image == "nginx" && + *pod.Spec.ActiveDeadlineSeconds == 30 && + pod.Annotations["annotation1"] == "value_annotation1" && + pod.Annotations["annotation2"] == "value_annotation2_new" && + pod.Annotations["annotation3"] == "value_annotation3" && + pod.Labels["label1"] == "value_label1_new" && + pod.Labels["label2"] == "value_label2" && + pod.Labels["label3"] == "value_label3" + }, timeout, interval).Should(BeTrue()) + }) + }) +}) diff --git a/controllers/template.ericogr.github.com/object_utilities.go b/controllers/template/object_utilities.go similarity index 100% rename from controllers/template.ericogr.github.com/object_utilities.go rename to controllers/template/object_utilities.go diff --git a/controllers/template.ericogr.github.com/objecttemplate_controller.go b/controllers/template/objecttemplate_controller.go similarity index 93% rename from controllers/template.ericogr.github.com/objecttemplate_controller.go rename to controllers/template/objecttemplate_controller.go index 65aa3a7..3630e6d 100644 --- a/controllers/template.ericogr.github.com/objecttemplate_controller.go +++ b/controllers/template/objecttemplate_controller.go @@ -28,7 +28,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/predicate" - otv1 "github.com/ericogr/k8s-object-template/apis/template.ericogr.github.com/v1" + otv1 "github.com/ericogr/k8s-object-template/apis/v1" ) // ObjectTemplateReconciler ot reconciler @@ -46,8 +46,8 @@ func (r *ObjectTemplateReconciler) SetupWithManager(mgr ctrl.Manager) error { Complete(r) } -// +kubebuilder:rbac:groups=template.ericogr.github.com,resources=objecttemplates,verbs=get;list;watch;create;update;patch;delete -// +kubebuilder:rbac:groups=template.ericogr.github.com,resources=objecttemplates/status,verbs=get;update;patch +// +kubebuilder:rbac:groups=template.k8s.ericogr.com.br,resources=objecttemplates,verbs=get;list;watch;create;update;patch;delete +// +kubebuilder:rbac:groups=template.k8s.ericogr.com.br,resources=objecttemplates/status,verbs=get;update;patch // Reconcile k8s reconcile func (r *ObjectTemplateReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { diff --git a/controllers/template.ericogr.github.com/objecttemplateparams_controller.go b/controllers/template/objecttemplateparams_controller.go similarity index 93% rename from controllers/template.ericogr.github.com/objecttemplateparams_controller.go rename to controllers/template/objecttemplateparams_controller.go index ac12dc2..6c4adf8 100644 --- a/controllers/template.ericogr.github.com/objecttemplateparams_controller.go +++ b/controllers/template/objecttemplateparams_controller.go @@ -29,7 +29,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/predicate" - otv1 "github.com/ericogr/k8s-object-template/apis/template.ericogr.github.com/v1" + otv1 "github.com/ericogr/k8s-object-template/apis/v1" ) var ( @@ -51,8 +51,8 @@ func (r *ObjectTemplateParamsReconciler) SetupWithManager(mgr ctrl.Manager) erro Complete(r) } -// +kubebuilder:rbac:groups=template.ericogr.github.com,resources=objecttemplateparams,verbs=get;list;watch;create;update;patch;delete -// +kubebuilder:rbac:groups=template.ericogr.github.com,resources=objecttemplateparams/status,verbs=get;update;patch +// +kubebuilder:rbac:groups=template.k8s.ericogr.com.br,resources=objecttemplateparams,verbs=get;list;watch;create;update;patch;delete +// +kubebuilder:rbac:groups=template.k8s.ericogr.com.br,resources=objecttemplateparams/status,verbs=get;update;patch // Reconcile reconcile func (r *ObjectTemplateParamsReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { diff --git a/controllers/template.ericogr.github.com/suite_test.go b/controllers/template/suite_test.go similarity index 97% rename from controllers/template.ericogr.github.com/suite_test.go rename to controllers/template/suite_test.go index 895a74c..b3048db 100644 --- a/controllers/template.ericogr.github.com/suite_test.go +++ b/controllers/template/suite_test.go @@ -31,7 +31,7 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" - otv1 "github.com/ericogr/k8s-object-template/apis/template.ericogr.github.com/v1" + otv1 "github.com/ericogr/k8s-object-template/apis/v1" // +kubebuilder:scaffold:imports ) diff --git a/main.go b/main.go index 5d58ee0..e20cedc 100644 --- a/main.go +++ b/main.go @@ -26,9 +26,8 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/log/zap" - otv1 "github.com/ericogr/k8s-object-template/apis/template.ericogr.github.com/v1" - controllers "github.com/ericogr/k8s-object-template/controllers/template.ericogr.github.com" - otc "github.com/ericogr/k8s-object-template/controllers/template.ericogr.github.com" + otv1 "github.com/ericogr/k8s-object-template/apis/v1" + controllers "github.com/ericogr/k8s-object-template/controllers/template" // +kubebuilder:scaffold:imports ) @@ -59,7 +58,7 @@ func main() { MetricsBindAddress: metricsAddr, Port: 9443, LeaderElection: enableLeaderElection, - LeaderElectionID: "6c0a8208.github.com", + LeaderElectionID: "object-template-leader.github.com", }) if err != nil { setupLog.Error(err, "unable to start manager") @@ -75,7 +74,7 @@ func main() { os.Exit(1) } - if err = (&otc.ObjectTemplateParamsReconciler{ + if err = (&controllers.ObjectTemplateParamsReconciler{ Client: mgr.GetClient(), Log: ctrl.Log.WithName("controllers").WithName("ObjectTemplateParams"), Scheme: mgr.GetScheme(), diff --git a/specs/object-template-operator.yaml b/specs/object-template-operator.yaml index 9019c8e..b283173 100644 --- a/specs/object-template-operator.yaml +++ b/specs/object-template-operator.yaml @@ -11,7 +11,7 @@ metadata: annotations: controller-gen.kubebuilder.io/version: v0.2.5 creationTimestamp: null - name: objecttemplateparams.template.ericogr.github.com + name: objecttemplateparams.template.k8s.ericogr.com.br spec: additionalPrinterColumns: - JSONPath: .status.status @@ -20,7 +20,7 @@ spec: - JSONPath: .metadata.creationTimestamp name: age type: date - group: template.ericogr.github.com + group: template.k8s.ericogr.com.br names: kind: ObjectTemplateParams listKind: ObjectTemplateParamsList @@ -93,7 +93,7 @@ metadata: annotations: controller-gen.kubebuilder.io/version: v0.2.5 creationTimestamp: null - name: objecttemplates.template.ericogr.github.com + name: objecttemplates.template.k8s.ericogr.com.br spec: additionalPrinterColumns: - JSONPath: .status.status @@ -102,7 +102,7 @@ spec: - JSONPath: .metadata.creationTimestamp name: age type: date - group: template.ericogr.github.com + group: template.k8s.ericogr.com.br names: kind: ObjectTemplate listKind: ObjectTemplateList @@ -238,7 +238,7 @@ rules: - update - watch - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplateparams verbs: @@ -250,7 +250,7 @@ rules: - update - watch - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplateparams/status verbs: @@ -258,7 +258,7 @@ rules: - patch - update - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplates verbs: @@ -270,7 +270,7 @@ rules: - update - watch - apiGroups: - - template.ericogr.github.com + - template.k8s.ericogr.com.br resources: - objecttemplates/status verbs: