Skip to content

Commit

Permalink
fix(drift): helm hook check
Browse files Browse the repository at this point in the history
  • Loading branch information
maxime1907 committed Sep 26, 2023
1 parent 6eba848 commit 6c6aeae
Showing 1 changed file with 52 additions and 10 deletions.
62 changes: 52 additions & 10 deletions pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,70 @@ func (resource *Resource) GetNameSpace(name, kind, dataMap string) (string, erro
return "", nil
}

func isNestedKeyNotNil(data map[string]interface{}, key string) bool {
if len(data) == 0 {
return false
}

keys := splitKey(key, ".", "\\")

// Traverse the nested structure
for i, k := range keys {
value, ok := data[k]
if !ok || value == nil {
return false
}
if nestedMap, ok := value.(map[string]interface{}); ok {
data = nestedMap
} else if i+1 < len(keys) {
// Key does not point to a map, so we can't check deeper.
return false
} else {
// Last key is valid and not nil.
return true
}
}
return false
}

func splitKey(key string, delimiter string, escapedchar string) []string {
// Split the key using the specified delimiter
parts := strings.Split(key, delimiter)

// Merge any escaped delimiters with the previous part
var result []string
for i := 0; i < len(parts); i++ {
if strings.HasSuffix(parts[i], escapedchar) {
// Remove the trailing backslash and merge it with the next part
parts[i] = strings.TrimSuffix(parts[i], escapedchar)
if i+1 < len(parts) {
result = append(result, parts[i]+delimiter+parts[i+1])
i++ // Skip the next part
} else {
// If there's no next part, just append the escaped part
result = append(result, parts[i])
}
} else {
result = append(result, parts[i])
}
}
return result
}

// IsHelmHook gets the namespace form the kubernetes resource.
func (resource *Resource) IsHelmHook(dataMap string, hookKinds []string) (bool, error) {
var kindYaml map[string]interface{}
if err := yaml.Unmarshal([]byte(dataMap), &kindYaml); err != nil {
return false, err
}

if len(kindYaml) == 0 {
if !isNestedKeyNotNil(kindYaml, "metadata.annotations.helm\\.sh/hook") || !isNestedKeyNotNil(kindYaml, "metadata.annotations.helm\\.sh/hook-delete-policy") {
return false, nil
}

if _, failedManifest := kindYaml["metadata"].(map[string]interface{})["annotations"]; !failedManifest {
return false, nil
}

if _, failedManifest := kindYaml["metadata"].(map[string]interface{})["annotations"].(map[string]interface{})["helm.sh/hook"].(string); !failedManifest {
return false, &errors.NotFoundError{Key: "failed to identify the manifest as chart hook"}
}

hookType, failedManifest := kindYaml["metadata"].(map[string]interface{})["annotations"].(map[string]interface{})["helm.sh/hook-delete-policy"].(string)
if !failedManifest {
return false, &errors.NotFoundError{Key: "failed to identify the the chart hook type from the manifest"}
return false, nil
}

hookType = strings.TrimSpace(hookType)
Expand Down

0 comments on commit 6c6aeae

Please sign in to comment.