Skip to content

Commit

Permalink
Propagate Application labels to application manifest as way to define…
Browse files Browse the repository at this point in the history
… environment-specific values for monitoring, logging, incident-management
  • Loading branch information
alexandrKuzmitsky committed Dec 13, 2022
1 parent c4c6bfa commit 4c3a897
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
9 changes: 9 additions & 0 deletions reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import (
"github.com/argoproj/argo-cd/v2/util/helm"
"github.com/argoproj/argo-cd/v2/util/io"
pathutil "github.com/argoproj/argo-cd/v2/util/io/path"
argokube "github.com/argoproj/argo-cd/v2/util/kube"
"github.com/argoproj/argo-cd/v2/util/kustomize"
"github.com/argoproj/argo-cd/v2/util/text"
)
Expand Down Expand Up @@ -1137,11 +1138,19 @@ func GenerateManifests(ctx context.Context, appPath, repoRoot, revision string,
}

for _, target := range targets {

if q.AppLabelKey != "" && q.AppName != "" && !kube.IsCRD(target) {
err = resourceTracking.SetAppInstance(target, q.AppLabelKey, q.AppName, q.Namespace, v1alpha1.TrackingMethod(q.TrackingMethod))
if err != nil {
return nil, err
}
//Iterate over Application labels and set them into manifest.
for k, v := range target.GetLabels() {
err := argokube.SetAppLabel(target, k, v)
if err != nil {
return nil, err
}
}
}
manifestStr, err := json.Marshal(target.Object)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions util/argo/resource_tracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ResourceTracking interface {
Normalize(config, live *unstructured.Unstructured, labelKey, trackingMethod string) error
}

//AppInstanceValue store information about resource tracking info
// AppInstanceValue store information about resource tracking info
type AppInstanceValue struct {
ApplicationName string
Group string
Expand Down Expand Up @@ -129,7 +129,7 @@ func (rt *resourceTracking) SetAppInstance(un *unstructured.Unstructured, key, v
}
switch trackingMethod {
case TrackingMethodLabel:
return argokube.SetAppInstanceLabel(un, key, val)
return argokube.SetAppLabel(un, key, val)
case TrackingMethodAnnotation:
return setAppInstanceAnnotation()
case TrackingMethodAnnotationAndLabel:
Expand All @@ -140,18 +140,18 @@ func (rt *resourceTracking) SetAppInstance(un *unstructured.Unstructured, key, v
if len(val) > LabelMaxLength {
val = val[:LabelMaxLength]
}
return argokube.SetAppInstanceLabel(un, key, val)
return argokube.SetAppLabel(un, key, val)
default:
return argokube.SetAppInstanceLabel(un, key, val)
return argokube.SetAppLabel(un, key, val)
}
}

//BuildAppInstanceValue build resource tracking id in format <application-name>;<group>/<kind>/<namespace>/<name>
// BuildAppInstanceValue build resource tracking id in format <application-name>;<group>/<kind>/<namespace>/<name>
func (rt *resourceTracking) BuildAppInstanceValue(value AppInstanceValue) string {
return fmt.Sprintf("%s:%s/%s:%s/%s", value.ApplicationName, value.Group, value.Kind, value.Namespace, value.Name)
}

//ParseAppInstanceValue parse resource tracking id from format <application-name>:<group>/<kind>:<namespace>/<name> to struct
// ParseAppInstanceValue parse resource tracking id from format <application-name>:<group>/<kind>:<namespace>/<name> to struct
func (rt *resourceTracking) ParseAppInstanceValue(value string) (*AppInstanceValue, error) {
var appInstanceValue AppInstanceValue
parts := strings.Split(value, ":")
Expand Down
2 changes: 1 addition & 1 deletion util/argo/resource_tracking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/argoproj/argo-cd/v2/common"
)

func TestSetAppInstanceLabel(t *testing.T) {
func TestSetAppLabel(t *testing.T) {
yamlBytes, err := os.ReadFile("testdata/svc.yaml")
assert.Nil(t, err)

Expand Down
4 changes: 2 additions & 2 deletions util/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func IsValidResourceName(name string) bool {
return len(name) < 64 && resourceNamePattern.MatchString(name)
}

// SetAppInstanceLabel the recommended app.kubernetes.io/instance label against an unstructured object
// SetAppLabel the recommended app.kubernetes.io/instance label against an unstructured object
// Uses the legacy labeling if environment variable is set
func SetAppInstanceLabel(target *unstructured.Unstructured, key, val string) error {
func SetAppLabel(target *unstructured.Unstructured, key, val string) error {
labels := target.GetLabels()
if labels == nil {
labels = make(map[string]string)
Expand Down

0 comments on commit 4c3a897

Please sign in to comment.