-
Notifications
You must be signed in to change notification settings - Fork 34
/
inputmanifest.go
61 lines (54 loc) · 2.25 KB
/
inputmanifest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package testingframework
import (
"fmt"
comm "github.com/berops/claudie/internal/command"
"github.com/berops/claudie/internal/envs"
"github.com/berops/claudie/internal/kubectl"
"github.com/berops/claudie/internal/manifest"
v1beta "github.com/berops/claudie/services/claudie-operator/pkg/api/v1beta1"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
yaml "k8s.io/apimachinery/pkg/util/yaml"
)
// deleteInputManifest will delete an inputManifest from the cluster in the specified namespace
func deleteInputManifest(name string) error {
kc := kubectl.Kubectl{MaxKubectlRetries: 3}
if log.Logger.GetLevel() == zerolog.DebugLevel {
kc.Stdout = comm.GetStdOut(name)
kc.Stderr = comm.GetStdErr(name)
}
return kc.KubectlDeleteResource("inputmanifest", name, "-n", envs.Namespace)
}
// applyInputManifest function will create a inputManifest resource from the yaml file
// the name of the resource will be defined already in the file
func applyInputManifest(yamlFile []byte, pathToTestSet string) error {
kc := kubectl.Kubectl{MaxKubectlRetries: 3}
if log.Logger.GetLevel() == zerolog.DebugLevel {
kc.Stdout = comm.GetStdOut(pathToTestSet)
kc.Stderr = comm.GetStdErr(pathToTestSet)
}
return kc.KubectlApplyString(string(yamlFile), "-n", envs.Namespace)
}
// getInputManifestName will read the name of the manifest from the file and return it,
// so it can be used as an id to retrieve it from database in configChecker()
func getInputManifestName(yamlFile []byte) (string, error) {
// Local testing
if envs.Namespace == "" {
var manifest manifest.Manifest
if err := yaml.Unmarshal(yamlFile, &manifest); err != nil {
return "", fmt.Errorf("error while unmarshalling a manifest file: %w", err)
}
log.Debug().Msgf("Returning name %s for", manifest.Name)
return manifest.Name, nil
}
var manifest v1beta.InputManifest
err := yaml.Unmarshal(yamlFile, &manifest)
if err != nil {
return "", fmt.Errorf("error while unmarshalling a manifest file: %w", err)
}
// Name is checked before apply, so ID needs to be combined manually (.metadata.namespace is not present before apply)
if manifest.GetName() != "" {
return envs.Namespace + "-" + manifest.GetName(), nil
}
return "", fmt.Errorf("manifest does not have a name defined, which could be used as DB id")
}