Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
This will accpet input of appversion of any kind like
Browse files Browse the repository at this point in the history
integer or float or string without quotes
Here using a special type to allow marshalling and
git commit -am
  • Loading branch information
piyush-garg committed Nov 23, 2017
1 parent 49b7886 commit c916a18
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 32 deletions.
42 changes: 42 additions & 0 deletions pkg/spec/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (

"github.com/ghodss/yaml"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"regexp"
)

// Every controller that Kedge supports is required to implement this interface
Expand All @@ -45,6 +47,7 @@ type ControllerInterface interface {
// Returns an error if the controller is not supported by Kedge
func GetController(data []byte) (ControllerInterface, error) {
var specController Controller

yaml.Unmarshal(data, &specController)

switch strings.ToLower(specController.Controller) {
Expand Down Expand Up @@ -96,5 +99,44 @@ func CoreOperations(data []byte) ([]runtime.Object, []string, error) {
return nil, nil, errors.Wrap(err, "unable to transform data")
}

//Regex to find the location of appversion
//Trying to find the location of \nappversion
//so it always be at root level
location := regexp.MustCompile("(^|\n)appversion").FindIndex(data)

//if location is nil then appversion is not given
if location != nil {
input := string(data)
i := location[1]
var value string
//reading the value of appversion field
for ; ; i++ {
if (input[i] == ' ') || (input[i] == ':') {
continue
}
if input[i] == '\n' {
break
}
value = value + string(input[i])
}

//adding key value pair of appversion to metadata of every object
//checking of the input is not empty
if value != "" {
accessor := meta.NewAccessor()
for _, ro := range ros {
annotations, err := accessor.Annotations(ro)
if err != nil {
return nil, nil, errors.Wrap(err, "cannot get annotations")
}
annotations = addKeyValueToMap(appVersion, value, annotations)
err = accessor.SetAnnotations(ro, annotations)
if err != nil {
return nil, nil, errors.Wrap(err, "cannot set annotations")
}
}
}
}

return ros, includeResources, nil
}
5 changes: 0 additions & 5 deletions pkg/spec/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ 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
}

Expand Down Expand Up @@ -137,8 +134,6 @@ 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,
Expand Down
6 changes: 0 additions & 6 deletions pkg/spec/deploymentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ 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)
Expand Down
3 changes: 0 additions & 3 deletions pkg/spec/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ func (job *JobSpecMod) Fix() error {
job.RestartPolicy = api_v1.RestartPolicyOnFailure
}

if job.ControllerFields.Appversion != "" {
job.ControllerFields.ObjectMeta.Annotations = addKeyValueToMap(appVersion, job.ControllerFields.Appversion, job.ControllerFields.ObjectMeta.Annotations)
}
return nil
}

Expand Down
17 changes: 0 additions & 17 deletions pkg/spec/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ 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"
Expand Down Expand Up @@ -587,22 +586,6 @@ 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 the metadata of all the objects based on their type
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
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/spec/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ type BuildConfigSpecMod struct {
type ControllerFields struct {
// Field to specify the version of application
// +optional
Appversion string `json:"appversion,omitempty"`
Appversion *AppVersionType `json:"appversion,omitempty"`

Controller string `json:"controller,omitempty"`
// List of volume that should be mounted on the pod.
Expand Down Expand Up @@ -253,3 +253,7 @@ type DeploymentConfigSpecMod struct {
// *int32 helps us do it, followed by substitution later on.
Replicas *int32 `json:"replicas,omitempty"`
}

//ApppversionType is declared to not give error input at the time of unmarshalling
//if appversion kept as string it will throw error if user give input int or float
type AppVersionType string
8 changes: 8 additions & 0 deletions pkg/spec/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,11 @@ func addKeyValueToMap(k string, v string, m map[string]string) map[string]string

return m
}

//this will just convert whatever input given as appversion to string
//and set the value of appversion so it will not give error at the time
//of unmarshalling
func (fs *AppVersionType) UnmarshalJSON(value []byte) error {
*fs = AppVersionType(string(value))
return nil
}

0 comments on commit c916a18

Please sign in to comment.