Skip to content

Commit

Permalink
fix: redundant setup ns in kustomization (#239)
Browse files Browse the repository at this point in the history
* feat: stop setup namespace for kustomize in all the cases

* feat: stop setup namespace for kustomize in all the cases

* bump version

* feat: ability to disable and enable kustomization namespace

* feat: ability to disable and enable kustomization namespace

* feat: ability to disable and enable kustomization namespace
  • Loading branch information
pasha-codefresh authored Oct 4, 2023
1 parent 400fa73 commit 517e6f6
Show file tree
Hide file tree
Showing 16 changed files with 570 additions and 505 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.8.1-cap-CR-player-and-vuln-fix
2.8.1-cap-CR-not-setup-kustomize-ns
3 changes: 3 additions & 0 deletions assets/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -7500,6 +7500,9 @@
"buildOptions": {
"type": "string",
"title": "BuildOptions is a string of build parameters to use when calling `kustomize build`"
},
"setNamespace": {
"type": "boolean"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion controller/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (m *appStateManager) getRepoObjs(app *v1alpha1.Application, sources []v1alp
if err != nil {
return nil, nil, fmt.Errorf("failed to get repo %q: %w", source.RepoURL, err)
}
kustomizeOptions, err := kustomizeSettings.GetOptions(source)
kustomizeOptions, err := kustomizeSettings.GetOptions(source, m.settingsMgr.GetKustomizeSetNamespaceEnabled())
if err != nil {
return nil, nil, fmt.Errorf("failed to get Kustomize options for source %d of %d: %w", i+1, len(sources), err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/api-rules/violation_exceptions.list
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ API rule violation: names_match,github.com/argoproj/argo-cd/v2/pkg/apis/applicat
API rule violation: names_match,github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1,JWTToken,IssuedAt
API rule violation: names_match,github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1,KustomizeOptions,BinaryPath
API rule violation: names_match,github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1,KustomizeOptions,BuildOptions
API rule violation: names_match,github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1,KustomizeOptions,SetNamespace
API rule violation: names_match,github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1,PullRequestGenerator,AzureDevOps
API rule violation: names_match,github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1,PullRequestGenerator,GitLab
API rule violation: names_match,github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1,RefTarget,Chart
Expand Down
1,013 changes: 522 additions & 491 deletions pkg/apis/application/v1alpha1/generated.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pkg/apis/application/v1alpha1/generated.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion pkg/apis/application/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/apis/application/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2602,6 +2602,8 @@ type KustomizeOptions struct {
BuildOptions string `protobuf:"bytes,1,opt,name=buildOptions"`
// BinaryPath holds optional path to kustomize binary
BinaryPath string `protobuf:"bytes,2,opt,name=binaryPath"`

SetNamespace bool `protobuf:"varint,3,opt,name=setNamespace"`
}

// CascadedDeletion indicates if the deletion finalizer is set and controller should delete the application and it's cascaded resources
Expand Down
2 changes: 1 addition & 1 deletion server/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func (s *Server) queryRepoServer(ctx context.Context, a *appv1.Application, acti
if err != nil {
return fmt.Errorf("error getting kustomize settings: %w", err)
}
kustomizeOptions, err := kustomizeSettings.GetOptions(a.Spec.GetSource())
kustomizeOptions, err := kustomizeSettings.GetOptions(a.Spec.GetSource(), s.settingsMgr.GetKustomizeSetNamespaceEnabled())
if err != nil {
return fmt.Errorf("error getting kustomize settings options: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion server/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func (s *Server) GetAppDetails(ctx context.Context, q *repositorypkg.RepoAppDeta
if err != nil {
return nil, err
}
kustomizeOptions, err := kustomizeSettings.GetOptions(*q.Source)
kustomizeOptions, err := kustomizeSettings.GetOptions(*q.Source, s.settings.GetKustomizeSetNamespaceEnabled())
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion util/argo/argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ func verifyGenerateManifests(
})
continue
}
kustomizeOptions, err := kustomizeSettings.GetOptions(source)
kustomizeOptions, err := kustomizeSettings.GetOptions(source, settingsMgr.GetKustomizeSetNamespaceEnabled())
if err != nil {
conditions = append(conditions, argoappv1.ApplicationCondition{
Type: argoappv1.ApplicationConditionInvalidSpecError,
Expand Down
2 changes: 1 addition & 1 deletion util/argo/argo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func TestValidateRepo(t *testing.T) {
GroupKind: schema.GroupKind{Kind: "Deployment"},
}}
kubeVersion := "v1.16"
kustomizeOptions := &argoappv1.KustomizeOptions{BuildOptions: ""}
kustomizeOptions := &argoappv1.KustomizeOptions{BuildOptions: "", SetNamespace: true}
repo := &argoappv1.Repository{Repo: fmt.Sprintf("file://%s", repoPath)}
cluster := &argoappv1.Cluster{Server: "sample server"}
app := &argoappv1.Application{
Expand Down
2 changes: 1 addition & 1 deletion util/kustomize/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp

env = append(env, environ...)

if namespace != "" {
if kustomizeOptions != nil && kustomizeOptions.SetNamespace && namespace != "" {
cmd := exec.Command(k.getBinaryPath(), "edit", "set", "namespace", "--", namespace)
cmd.Dir = k.path
_, err := executil.Run(cmd)
Expand Down
2 changes: 1 addition & 1 deletion util/notification/argocd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (svc *argoCDService) getKustomizeOptions(source *v1alpha1.ApplicationSource
if err != nil {
return nil, err
}
return kustomizeSettings.GetOptions(*source)
return kustomizeSettings.GetOptions(*source, svc.settingsMgr.GetKustomizeSetNamespaceEnabled())
}

func (svc *argoCDService) GetAppDetails(ctx context.Context, appSource *v1alpha1.ApplicationSource) (*shared.AppDetail, error) {
Expand Down
21 changes: 20 additions & 1 deletion util/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ type ArgoCDSettings struct {
// ExtensionConfig configurations related to ArgoCD proxy extensions. The value
// is a yaml string defined in extension.ExtensionConfigs struct.
ExtensionConfig string `json:"extensionConfig,omitempty"`
// KustomizeSetNamespaceEnabled enable set namespace for kustomize by default
KustomizeSetNamespaceEnabled bool `json:"kustomizeSetNamespaceEnabled"`
}

type GoogleAnalytics struct {
Expand Down Expand Up @@ -257,7 +259,7 @@ var (
}
)

func (ks *KustomizeSettings) GetOptions(source v1alpha1.ApplicationSource) (*v1alpha1.KustomizeOptions, error) {
func (ks *KustomizeSettings) GetOptions(source v1alpha1.ApplicationSource, setNamespace bool) (*v1alpha1.KustomizeOptions, error) {
binaryPath := ""
buildOptions := ""
if source.Kustomize != nil && source.Kustomize.Version != "" {
Expand All @@ -279,6 +281,7 @@ func (ks *KustomizeSettings) GetOptions(source v1alpha1.ApplicationSource) (*v1a
return &v1alpha1.KustomizeOptions{
BuildOptions: buildOptions,
BinaryPath: binaryPath,
SetNamespace: setNamespace,
}, nil
}

Expand Down Expand Up @@ -482,6 +485,8 @@ const (
// ResourceDeepLinks is the resource deep link key
ResourceDeepLinks = "resource.links"
extensionConfig = "extension.config"
// kustomizeSetNamespaceEnabledKey is the key to configure if kustomize set namespace should be executed
kustomizeSetNamespaceEnabledKey = "kustomize.setNamespace.enabled"
)

var (
Expand Down Expand Up @@ -715,6 +720,19 @@ func (mgr *SettingsManager) GetAppInstanceLabelKey() (string, error) {
return label, nil
}

func (mgr *SettingsManager) GetKustomizeSetNamespaceEnabled() bool {
argoCDCM, err := mgr.getConfigMap()
if err != nil {
return true
}
kustomizeSetNamespaceEnabled := argoCDCM.Data[kustomizeSetNamespaceEnabledKey]
if kustomizeSetNamespaceEnabled == "" {
// enabled by default because it is a breaking change to disable it
return true
}
return kustomizeSetNamespaceEnabled == "true"
}

func (mgr *SettingsManager) GetTrackingMethod() (string, error) {
argoCDCM, err := mgr.getConfigMap()
if err != nil {
Expand Down Expand Up @@ -1406,6 +1424,7 @@ func updateSettingsFromConfigMap(settings *ArgoCDSettings, argoCDCM *apiv1.Confi
}
settings.InClusterEnabled = argoCDCM.Data[inClusterEnabledKey] != "false"
settings.ExecEnabled = argoCDCM.Data[execEnabledKey] == "true"

execShells := argoCDCM.Data[execShellsKey]
if execShells != "" {
settings.ExecShells = strings.Split(execShells, ",")
Expand Down
8 changes: 4 additions & 4 deletions util/settings/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,12 +723,12 @@ func TestKustomizeSettings_GetOptions(t *testing.T) {

t.Run("VersionDoesNotExist", func(t *testing.T) {
_, err := settings.GetOptions(v1alpha1.ApplicationSource{
Kustomize: &v1alpha1.ApplicationSourceKustomize{Version: "v4"}})
Kustomize: &v1alpha1.ApplicationSourceKustomize{Version: "v4"}}, true)
assert.Error(t, err)
})

t.Run("DefaultBuildOptions", func(t *testing.T) {
ver, err := settings.GetOptions(v1alpha1.ApplicationSource{})
ver, err := settings.GetOptions(v1alpha1.ApplicationSource{}, true)
if !assert.NoError(t, err) {
return
}
Expand All @@ -738,7 +738,7 @@ func TestKustomizeSettings_GetOptions(t *testing.T) {

t.Run("VersionExists", func(t *testing.T) {
ver, err := settings.GetOptions(v1alpha1.ApplicationSource{
Kustomize: &v1alpha1.ApplicationSourceKustomize{Version: "v2"}})
Kustomize: &v1alpha1.ApplicationSourceKustomize{Version: "v2"}}, true)
if !assert.NoError(t, err) {
return
}
Expand All @@ -748,7 +748,7 @@ func TestKustomizeSettings_GetOptions(t *testing.T) {

t.Run("VersionExistsWithBuildOption", func(t *testing.T) {
ver, err := settings.GetOptions(v1alpha1.ApplicationSource{
Kustomize: &v1alpha1.ApplicationSourceKustomize{Version: "v3"}})
Kustomize: &v1alpha1.ApplicationSourceKustomize{Version: "v3"}}, true)
if !assert.NoError(t, err) {
return
}
Expand Down

0 comments on commit 517e6f6

Please sign in to comment.