diff --git a/pkg/app/pipedv1/plugin/kubernetes_multicluster/config/application.go b/pkg/app/pipedv1/plugin/kubernetes_multicluster/config/application.go index ca51727db0..a76d92317e 100644 --- a/pkg/app/pipedv1/plugin/kubernetes_multicluster/config/application.go +++ b/pkg/app/pipedv1/plugin/kubernetes_multicluster/config/application.go @@ -78,7 +78,14 @@ func (s *KubernetesApplicationSpec) UnmarshalJSON(data []byte) error { } func (s *KubernetesApplicationSpec) Validate() error { - // TODO: Validate KubernetesApplicationSpec fields. + if s.Input.HelmChart != nil && len(s.Input.KustomizeOptions) > 0 { + return errors.New("helmChart and kustomizeOptions are mutually exclusive") + } + for i, mt := range s.Input.MultiTargets { + if mt.Target.Name == "" { + return fmt.Errorf("multiTargets[%d].target.name must not be empty", i) + } + } return nil } diff --git a/pkg/app/pipedv1/plugin/kubernetes_multicluster/config/application_test.go b/pkg/app/pipedv1/plugin/kubernetes_multicluster/config/application_test.go index 61e72fb01c..55b010d96f 100644 --- a/pkg/app/pipedv1/plugin/kubernetes_multicluster/config/application_test.go +++ b/pkg/app/pipedv1/plugin/kubernetes_multicluster/config/application_test.go @@ -97,3 +97,75 @@ func TestFindDeployTarget(t *testing.T) { }) } } + +func TestKubernetesApplicationSpecValidate(t *testing.T) { + tests := []struct { + name string + spec KubernetesApplicationSpec + expectedErr string + }{ + { + name: "valid empty spec", + spec: KubernetesApplicationSpec{}, + }, + { + name: "valid single multiTarget", + spec: KubernetesApplicationSpec{ + Input: KubernetesDeploymentInput{ + MultiTargets: []KubernetesMultiTarget{ + {Target: KubernetesMultiTargetDeployTarget{Name: "cluster-us"}}, + }, + }, + }, + }, + { + name: "multiTarget with empty name", + spec: KubernetesApplicationSpec{ + Input: KubernetesDeploymentInput{ + MultiTargets: []KubernetesMultiTarget{ + {Target: KubernetesMultiTargetDeployTarget{Name: "cluster-us"}}, + {Target: KubernetesMultiTargetDeployTarget{Name: ""}}, + }, + }, + }, + expectedErr: "multiTargets[1].target.name must not be empty", + }, + { + name: "helmChart and kustomizeOptions are mutually exclusive", + spec: KubernetesApplicationSpec{ + Input: KubernetesDeploymentInput{ + HelmChart: &InputHelmChart{Path: "./charts/myapp"}, + KustomizeOptions: map[string]string{"enable-alpha-plugins": "true"}, + }, + }, + expectedErr: "helmChart and kustomizeOptions are mutually exclusive", + }, + { + name: "helmChart without kustomizeOptions is valid", + spec: KubernetesApplicationSpec{ + Input: KubernetesDeploymentInput{ + HelmChart: &InputHelmChart{Path: "./charts/myapp"}, + }, + }, + }, + { + name: "kustomizeOptions without helmChart is valid", + spec: KubernetesApplicationSpec{ + Input: KubernetesDeploymentInput{ + KustomizeOptions: map[string]string{"enable-alpha-plugins": "true"}, + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := tt.spec.Validate() + if tt.expectedErr != "" { + assert.EqualError(t, err, tt.expectedErr) + } else { + assert.NoError(t, err) + } + }) + } +}