Skip to content

Commit

Permalink
Manifest Definition Implementation (#113)
Browse files Browse the repository at this point in the history
* manifest definition, validation, and tests

* Merge fixes
  • Loading branch information
dbw7 authored Jan 18, 2024
1 parent d6029ee commit e51f535
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 10 deletions.
9 changes: 7 additions & 2 deletions pkg/image/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/image/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 11 additions & 8 deletions pkg/image/testdata/full-valid-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -65,3 +65,6 @@ embeddedArtifactRegistry:
kubernetes:
version: v1.29.0+rke2r1
nodeType: server
manifests:
urls:
- https://k8s.io/examples/application/nginx-app.yaml
26 changes: 26 additions & 0 deletions pkg/image/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
43 changes: 43 additions & 0 deletions pkg/image/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e51f535

Please sign in to comment.