From b8a42790cd2164c1f0ce1109b0760c383ca660b3 Mon Sep 17 00:00:00 2001 From: Piyush Garg Date: Wed, 15 Nov 2017 18:55:56 +0530 Subject: [PATCH] This will add a new key `appversion' which will get reflected in the metadata(as key-pair in annotation) of deployment, job and deploymentConfig. It is optional and also empty value of `appversion` will not be added to metadata #407 Formatted code according to gofmt Added annotations to all the objects in resources.go, changed the type of appversion to string, declared a constant for appversion Formatted according to gofmt Added generalized method for all objects and removed type switch Formatted according to gofmt This will accpet input of appversion of any kind like integer or float or string without quotes Here using a special type to allow marshalling and git commit -am Fallback to previous implementation, using Type as string and adding annotations in different places and user have to give input in quotes in case of int or float --- pkg/spec/deployment.go | 6 ++++++ pkg/spec/deploymentconfig.go | 6 ++++++ pkg/spec/job.go | 6 +++++- pkg/spec/resources.go | 19 +++++++++++++++++++ pkg/spec/types.go | 4 ++++ pkg/spec/util.go | 10 ++++------ 6 files changed, 44 insertions(+), 7 deletions(-) diff --git a/pkg/spec/deployment.go b/pkg/spec/deployment.go index 37ce6ae00..bddc507fc 100644 --- a/pkg/spec/deployment.go +++ b/pkg/spec/deployment.go @@ -56,6 +56,10 @@ func (deployment *DeploymentSpecMod) Fix() error { deployment.ControllerFields.ObjectMeta.Labels = addKeyValueToMap(appLabelKey, deployment.ControllerFields.Name, deployment.ControllerFields.ObjectMeta.Labels) + if deployment.ControllerFields.Appversion != "" { + deployment.ControllerFields.ObjectMeta.Annotations = addKeyValueToMap(appVersion, deployment.ControllerFields.Appversion, deployment.ControllerFields.ObjectMeta.Annotations) + } + return nil } @@ -134,6 +138,8 @@ func (deployment *DeploymentSpecMod) createKubernetesController() (*ext_v1beta1. // TODO: merge with already existing labels and avoid duplication deploymentSpec.Template.ObjectMeta.Labels = deployment.Labels + deploymentSpec.Template.ObjectMeta.Annotations = deployment.Annotations + return &ext_v1beta1.Deployment{ ObjectMeta: deployment.ObjectMeta, Spec: deploymentSpec, diff --git a/pkg/spec/deploymentconfig.go b/pkg/spec/deploymentconfig.go index 8d5b94913..52e58dbf6 100644 --- a/pkg/spec/deploymentconfig.go +++ b/pkg/spec/deploymentconfig.go @@ -65,6 +65,12 @@ func (deploymentConfig *DeploymentConfigSpecMod) fixDeploymentConfig() { deploymentConfig.ControllerFields.Name, deploymentConfig.ControllerFields.ObjectMeta.Labels) + if deploymentConfig.ControllerFields.Appversion != "" { + deploymentConfig.ControllerFields.ObjectMeta.Annotations = addKeyValueToMap(appVersion, + deploymentConfig.ControllerFields.Appversion, + deploymentConfig.ControllerFields.ObjectMeta.Annotations) + } + // If the replicas are not specified at all, we need to set the value as 1 if deploymentConfig.Replicas == nil { deploymentConfig.Replicas = getInt32Addr(1) diff --git a/pkg/spec/job.go b/pkg/spec/job.go index 998c90442..4096fb1a3 100644 --- a/pkg/spec/job.go +++ b/pkg/spec/job.go @@ -46,10 +46,15 @@ func (job *JobSpecMod) Fix() error { job.ControllerFields.ObjectMeta.Labels = addKeyValueToMap(appLabelKey, job.ControllerFields.Name, job.ControllerFields.ObjectMeta.Labels) + if job.ControllerFields.Appversion != "" { + job.ControllerFields.ObjectMeta.Annotations = addKeyValueToMap(appVersion, job.ControllerFields.Appversion, job.ControllerFields.ObjectMeta.Annotations) + } + // if RestartPolicy is not set by user default it to 'OnFailure' if job.RestartPolicy == "" { job.RestartPolicy = api_v1.RestartPolicyOnFailure } + return nil } @@ -136,7 +141,6 @@ func (job *JobSpecMod) Validate() error { if job.RestartPolicy == api_v1.RestartPolicyAlways { return fmt.Errorf("the Job %q is invalid: restartPolicy: unsupported value: \"Always\": supported values: OnFailure, Never", job.Name) } - return nil } diff --git a/pkg/spec/resources.go b/pkg/spec/resources.go index 85f0666b5..46c948116 100644 --- a/pkg/spec/resources.go +++ b/pkg/spec/resources.go @@ -31,6 +31,7 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/pkg/errors" + "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/intstr" @@ -44,6 +45,7 @@ import ( // allLabelKey is the key that Kedge injects in every Kubernetes resource that // it generates as an ObjectMeta label const appLabelKey = "app" +const appVersion = "appversion" // Fix @@ -585,6 +587,23 @@ func (app *ControllerFields) CreateK8sObjects() ([]runtime.Object, []string, err objects = append(objects, configMap...) log.Debugf("app: %s, configMap: %s\n", app.Name, spew.Sprint(configMap)) + //Adding annotations to all the resources + //Objects are runtimeobjects, so accessing them using meta library + if app.Appversion != "" { + accessor := meta.NewAccessor() + for _, object := range objects { + annotations, err := accessor.Annotations(object) + if err != nil { + return nil, nil, errors.Wrap(err, "cannot get annotations") + } + annotations = addKeyValueToMap(appVersion, app.Appversion, annotations) + err = accessor.SetAnnotations(object, annotations) + if err != nil { + return nil, nil, errors.Wrap(err, "cannot set annotations") + } + } + } + return objects, app.IncludeResources, nil } diff --git a/pkg/spec/types.go b/pkg/spec/types.go index 81d6d6deb..5087849a2 100644 --- a/pkg/spec/types.go +++ b/pkg/spec/types.go @@ -163,6 +163,10 @@ type BuildConfigSpecMod struct { // ControllerFields are the common fields in every controller Kedge supports type ControllerFields struct { + // Field to specify the version of application + // +optional + Appversion string `json:"appversion,omitempty"` + Controller string `json:"controller,omitempty"` // List of volume that should be mounted on the pod. // ref: io.kedge.VolumeClaim diff --git a/pkg/spec/util.go b/pkg/spec/util.go index 294c54aea..d4950778d 100644 --- a/pkg/spec/util.go +++ b/pkg/spec/util.go @@ -22,16 +22,14 @@ import ( "encoding/json" log "github.com/Sirupsen/logrus" - "github.com/pkg/errors" - "k8s.io/apimachinery/pkg/runtime" - //"k8s.io/apimachinery/pkg/util/intstr" - "k8s.io/kubernetes/pkg/api" - api_v1 "k8s.io/kubernetes/pkg/api/v1" - //kapi "k8s.io/kubernetes/pkg/api/v1" build_v1 "github.com/openshift/origin/pkg/build/apis/build/v1" os_deploy_v1 "github.com/openshift/origin/pkg/deploy/apis/apps/v1" image_v1 "github.com/openshift/origin/pkg/image/apis/image/v1" os_route_v1 "github.com/openshift/origin/pkg/route/apis/route/v1" + "github.com/pkg/errors" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/kubernetes/pkg/api" + api_v1 "k8s.io/kubernetes/pkg/api/v1" batch_v1 "k8s.io/kubernetes/pkg/apis/batch/v1" )