-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
56 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package kubeclient | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/client-go/dynamic" | ||
) | ||
|
||
func (c *Kubeclient) resourceInterfaceFor(obj *unstructured.Unstructured) (dynamic.ResourceInterface, error) { | ||
dyn := dynamic.New(c.client.RESTClient()) | ||
gvk := obj.GroupVersionKind() | ||
|
||
mapping, err := c.restMapper.RESTMapping(gvk.GroupKind(), gvk.Version) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get resource for %#v: %w", gvk, err) | ||
} | ||
c.log.Info("found mapping", "resource", mapping.Resource) | ||
ri := dyn.Resource(mapping.Resource) | ||
if namespace := obj.GetNamespace(); namespace != "" { | ||
return ri.Namespace(namespace), nil | ||
} | ||
return ri, nil | ||
} | ||
|
||
// Deploy a set of namespaced manifests to a namespace. | ||
func (c *Kubeclient) Deploy(ctx context.Context, manifests ...*unstructured.Unstructured) error { | ||
for _, obj := range manifests { | ||
ri, err := c.resourceInterfaceFor(obj) | ||
if err != nil { | ||
return err | ||
} | ||
applied, err := ri.Apply(ctx, obj.GetName(), obj, metav1.ApplyOptions{Force: true, FieldManager: "e2e-test"}) | ||
if err != nil { | ||
return fmt.Errorf("could not apply %s %s in namespace %s: %w", obj.GetKind(), obj.GetName(), obj.GetNamespace(), err) | ||
} | ||
c.log.Info("object applied", "namespace", applied.GetNamespace(), "kind", applied.GetKind(), "name", applied.GetName()) | ||
} | ||
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