...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/k8s/k8s.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/k8s

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testk8s
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"encoding/json"
    21  	"testing"
    22  
    23  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    24  
    25  	corev1 "k8s.io/api/core/v1"
    26  	"k8s.io/apimachinery/pkg/api/errors"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    29  	"k8s.io/apimachinery/pkg/runtime/schema"
    30  	"k8s.io/apimachinery/pkg/types"
    31  	"sigs.k8s.io/controller-runtime/pkg/client"
    32  	"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
    33  )
    34  
    35  func GetNamespace(t *testing.T, kubeClient client.Client, namespaceName string) *corev1.Namespace {
    36  	t.Helper()
    37  	namespace := corev1.Namespace{}
    38  	if err := kubeClient.Get(context.TODO(), types.NamespacedName{Name: namespaceName}, &namespace); err != nil {
    39  		t.Fatalf("error getting namespace: %v", err)
    40  	}
    41  	return &namespace
    42  }
    43  
    44  func RemoveDeletionDefenderFinalizer(t *testing.T, obj metav1.Object, gvk schema.GroupVersionKind, c client.Client) {
    45  	t.Helper()
    46  	nn := k8s.GetNamespacedName(obj)
    47  	u := &unstructured.Unstructured{}
    48  	u.SetGroupVersionKind(gvk)
    49  	if err := c.Get(context.Background(), nn, u); err != nil {
    50  		if errors.IsNotFound(err) {
    51  			return
    52  		}
    53  		t.Fatalf("error getting %v %v: %v", gvk, nn, err)
    54  	}
    55  	if !k8s.HasFinalizer(u, k8s.DeletionDefenderFinalizerName) {
    56  		return
    57  	}
    58  	k8s.RemoveFinalizer(u, k8s.DeletionDefenderFinalizerName)
    59  	if err := c.Update(context.Background(), u); err != nil {
    60  		t.Fatalf("error updating %v %v: %v", gvk, nn, err)
    61  	}
    62  }
    63  
    64  func RemoveDeletionDefenderFinalizerForUnstructured(t *testing.T, u *unstructured.Unstructured, c client.Client) {
    65  	t.Helper()
    66  	RemoveDeletionDefenderFinalizer(t, u, u.GroupVersionKind(), c)
    67  }
    68  
    69  func MapToFieldPathSet(t *testing.T, m map[string]interface{}) *fieldpath.Set {
    70  	b, err := json.Marshal(m)
    71  	if err != nil {
    72  		t.Fatal("error marshaling field path set JSON:", err)
    73  	}
    74  	res := fieldpath.NewSet()
    75  	if err := res.FromJSON(bytes.NewReader(b)); err != nil {
    76  		t.Fatal("error constructing expected set from JSON:", err)
    77  	}
    78  	return res
    79  }
    80  

View as plain text