-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve cli-utils manifest reader logic (#92)
cli-utils currently bails if a cluster scoped resource has a namespace field, which is not in-line with common helm behavior (many charts put namespaces in there even if they shouldn't). This reimplements the reader and namespace setting logic to just set the namespace to empty string and log that it was wrong originally.
- Loading branch information
1 parent
96808d4
commit 4254ef6
Showing
4 changed files
with
96 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package template | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"sigs.k8s.io/cli-utils/pkg/inventory" | ||
"sigs.k8s.io/cli-utils/pkg/manifestreader" | ||
"sigs.k8s.io/cli-utils/pkg/object" | ||
) | ||
|
||
func setNamespaces(mapper meta.RESTMapper, objs []*unstructured.Unstructured, | ||
defaultNamespace string, enforceNamespace bool) error { | ||
var crdObjs []*unstructured.Unstructured | ||
|
||
// find any crds in the set of resources. | ||
for _, obj := range objs { | ||
if object.IsCRD(obj) { | ||
crdObjs = append(crdObjs, obj) | ||
} | ||
} | ||
|
||
var unknownGVKs []schema.GroupVersionKind | ||
for _, obj := range objs { | ||
// Exclude any inventory objects here since we don't want to change | ||
// their namespace. | ||
if inventory.IsInventoryObject(obj) { | ||
continue | ||
} | ||
|
||
// Look up the scope of the resource so we know if the resource | ||
// should have a namespace set or not. | ||
scope, err := object.LookupResourceScope(obj, crdObjs, mapper) | ||
if err != nil { | ||
var unknownTypeError *object.UnknownTypeError | ||
if errors.As(err, &unknownTypeError) { | ||
// If no scope was found, just add the resource type to the list | ||
// of unknown types. | ||
unknownGVKs = append(unknownGVKs, unknownTypeError.GroupVersionKind) | ||
continue | ||
} | ||
// If something went wrong when looking up the scope, just | ||
// give up. | ||
return err | ||
} | ||
|
||
switch scope { | ||
case meta.RESTScopeNamespace: | ||
if obj.GetNamespace() == "" { | ||
obj.SetNamespace(defaultNamespace) | ||
} else { | ||
ns := obj.GetNamespace() | ||
if enforceNamespace && ns != defaultNamespace { | ||
return &manifestreader.NamespaceMismatchError{ | ||
Namespace: ns, | ||
RequiredNamespace: defaultNamespace, | ||
} | ||
} | ||
} | ||
case meta.RESTScopeRoot: | ||
if ns := obj.GetNamespace(); ns != "" { | ||
obj.SetNamespace("") | ||
fmt.Printf("Found cluster scoped resource %s with namespace %s, coerced to un-namespaced\n", obj.GetName(), ns) | ||
} | ||
default: | ||
return fmt.Errorf("unknown RESTScope %q", scope.Name()) | ||
} | ||
} | ||
if len(unknownGVKs) > 0 { | ||
return &manifestreader.UnknownTypesError{ | ||
GroupVersionKinds: unknownGVKs, | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters