diff --git a/pkg/image/definition.go b/pkg/image/definition.go index c3de85f5..d68b8319 100644 --- a/pkg/image/definition.go +++ b/pkg/image/definition.go @@ -120,8 +120,13 @@ type HelmChart struct { } type Kubernetes struct { - Version string `yaml:"version"` - NodeType string `yaml:"nodeType"` + Version string `yaml:"version"` + NodeType string `yaml:"nodeType"` + Manifests Manifests `yaml:"manifests"` +} + +type Manifests struct { + URLs []string `yaml:"urls"` } func ParseDefinition(data []byte) (*Definition, error) { diff --git a/pkg/image/definition_test.go b/pkg/image/definition_test.go index fb25ecb4..f6727ddf 100644 --- a/pkg/image/definition_test.go +++ b/pkg/image/definition_test.go @@ -134,6 +134,8 @@ func TestParse(t *testing.T) { kubernetes := definition.Kubernetes assert.Equal(t, "v1.29.0+rke2r1", kubernetes.Version) assert.Equal(t, "server", kubernetes.NodeType) + assert.Equal(t, "https://k8s.io/examples/application/nginx-app.yaml", kubernetes.Manifests.URLs[0]) + } func TestParseBadConfig(t *testing.T) { diff --git a/pkg/image/testdata/full-valid-example.yaml b/pkg/image/testdata/full-valid-example.yaml index 943435e6..32cb4df8 100644 --- a/pkg/image/testdata/full-valid-example.yaml +++ b/pkg/image/testdata/full-valid-example.yaml @@ -43,15 +43,15 @@ operatingSystem: getSSL: false packages: packageList: - - wget2 - - dpdk22 - - dpdk22-tools - - libdpdk-23 - - libatomic1 - - libbpf0 + - wget2 + - dpdk22 + - dpdk22-tools + - libdpdk-23 + - libatomic1 + - libbpf0 additionalRepos: - - https://download.nvidia.com/suse/sle15sp5/ - - https://developer.download.nvidia.com/compute/cuda/repos/sles15/x86_64/ + - https://download.nvidia.com/suse/sle15sp5/ + - https://developer.download.nvidia.com/compute/cuda/repos/sles15/x86_64/ registrationCode: INTERNAL-USE-ONLY-foo-bar embeddedArtifactRegistry: images: @@ -65,3 +65,6 @@ embeddedArtifactRegistry: kubernetes: version: v1.29.0+rke2r1 nodeType: server + manifests: + urls: + - https://k8s.io/examples/application/nginx-app.yaml diff --git a/pkg/image/validation.go b/pkg/image/validation.go index 680d4702..500ad173 100644 --- a/pkg/image/validation.go +++ b/pkg/image/validation.go @@ -97,6 +97,32 @@ func validateKubernetes(definition *Definition) error { return fmt.Errorf("unknown node type: %s", definition.Kubernetes.NodeType) } + err := validateManifestURLs(&definition.Kubernetes) + if err != nil { + return fmt.Errorf("validating manifest urls: %w", err) + } + + return nil +} + +func validateManifestURLs(kubernetes *Kubernetes) error { + if len(kubernetes.Manifests.URLs) == 0 { + return nil + } + seenManifests := make(map[string]bool) + + for _, manifest := range kubernetes.Manifests.URLs { + if !strings.HasPrefix(manifest, "http") { + return fmt.Errorf("invalid manifest url, does not start with 'http://' or 'https://'") + } + + if _, exists := seenManifests[manifest]; exists { + return fmt.Errorf("duplicate manifest url found: '%s'", manifest) + } + + seenManifests[manifest] = true + } + return nil } diff --git a/pkg/image/validation_test.go b/pkg/image/validation_test.go index 03ae764b..e935ab81 100644 --- a/pkg/image/validation_test.go +++ b/pkg/image/validation_test.go @@ -106,6 +106,49 @@ func TestValidateKubernetes(t *testing.T) { }, expectedErr: "unknown node type: worker", }, + { + name: "Valid manifest URLs", + definition: &Definition{ + Kubernetes: Kubernetes{ + Version: "v1.29.0+rke2r1", + Manifests: Manifests{ + URLs: []string{ + "https://k8s.io/examples/application/nginx-app.yaml", + "http://localhost:5000/manifest.yaml", + }, + }, + }, + }, + }, + { + name: "Duplicate manifest URLs", + definition: &Definition{ + Kubernetes: Kubernetes{ + Version: "v1.29.0+rke2r1", + Manifests: Manifests{ + URLs: []string{ + "https://k8s.io/examples/application/nginx-app.yaml", + "https://k8s.io/examples/application/nginx-app.yaml", + }, + }, + }, + }, + expectedErr: "validating manifest urls: duplicate manifest url found: 'https://k8s.io/examples/application/nginx-app.yaml'", + }, + { + name: "Invalid manifest URL", + definition: &Definition{ + Kubernetes: Kubernetes{ + Version: "v1.29.0+rke2r1", + Manifests: Manifests{ + URLs: []string{ + "k8s.io/examples/application/nginx-app.yaml", + }, + }, + }, + }, + expectedErr: "validating manifest urls: invalid manifest url, does not start with 'http://' or 'https://'", + }, } for _, test := range tests {