Upgrade k8s package verison (#5358)
* upgrade k8s package version Signed-off-by: hongzhouzi <hongzhouzi@kubesphere.io> * Script upgrade and code formatting. Signed-off-by: hongzhouzi <hongzhouzi@kubesphere.io> Signed-off-by: hongzhouzi <hongzhouzi@kubesphere.io>
This commit is contained in:
303
vendor/sigs.k8s.io/controller-runtime/pkg/envtest/crd.go
generated
vendored
303
vendor/sigs.k8s.io/controller-runtime/pkg/envtest/crd.go
generated
vendored
@@ -20,19 +20,16 @@ import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
||||
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
|
||||
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
@@ -42,9 +39,10 @@ import (
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/util/retry"
|
||||
"k8s.io/utils/pointer"
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/webhook/conversion"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// CRDInstallOptions are the options for installing CRDs.
|
||||
@@ -62,7 +60,7 @@ type CRDInstallOptions struct {
|
||||
Paths []string
|
||||
|
||||
// CRDs is a list of CRDs to install
|
||||
CRDs []client.Object
|
||||
CRDs []*apiextensionsv1.CustomResourceDefinition
|
||||
|
||||
// ErrorIfPathMissing will cause an error if a Path does not exist
|
||||
ErrorIfPathMissing bool
|
||||
@@ -90,7 +88,7 @@ const defaultPollInterval = 100 * time.Millisecond
|
||||
const defaultMaxWait = 10 * time.Second
|
||||
|
||||
// InstallCRDs installs a collection of CRDs into a cluster by reading the crd yaml files from a directory.
|
||||
func InstallCRDs(config *rest.Config, options CRDInstallOptions) ([]client.Object, error) {
|
||||
func InstallCRDs(config *rest.Config, options CRDInstallOptions) ([]*apiextensionsv1.CustomResourceDefinition, error) {
|
||||
defaultCRDOptions(&options)
|
||||
|
||||
// Read the CRD yamls into options.CRDs
|
||||
@@ -142,49 +140,14 @@ func defaultCRDOptions(o *CRDInstallOptions) {
|
||||
}
|
||||
|
||||
// WaitForCRDs waits for the CRDs to appear in discovery.
|
||||
func WaitForCRDs(config *rest.Config, crds []client.Object, options CRDInstallOptions) error {
|
||||
func WaitForCRDs(config *rest.Config, crds []*apiextensionsv1.CustomResourceDefinition, options CRDInstallOptions) error {
|
||||
// Add each CRD to a map of GroupVersion to Resource
|
||||
waitingFor := map[schema.GroupVersion]*sets.String{}
|
||||
for _, crd := range runtimeCRDListToUnstructured(crds) {
|
||||
for _, crd := range crds {
|
||||
gvs := []schema.GroupVersion{}
|
||||
crdGroup, _, err := unstructured.NestedString(crd.Object, "spec", "group")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
crdPlural, _, err := unstructured.NestedString(crd.Object, "spec", "names", "plural")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
crdVersion, _, err := unstructured.NestedString(crd.Object, "spec", "version")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
versions, found, err := unstructured.NestedSlice(crd.Object, "spec", "versions")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// gvs should be added here only if single version is found. If multiple version is found we will add those version
|
||||
// based on the version is served or not.
|
||||
if crdVersion != "" && !found {
|
||||
gvs = append(gvs, schema.GroupVersion{Group: crdGroup, Version: crdVersion})
|
||||
}
|
||||
|
||||
for _, version := range versions {
|
||||
versionMap, ok := version.(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
served, _, err := unstructured.NestedBool(versionMap, "served")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if served {
|
||||
versionName, _, err := unstructured.NestedString(versionMap, "name")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gvs = append(gvs, schema.GroupVersion{Group: crdGroup, Version: versionName})
|
||||
for _, version := range crd.Spec.Versions {
|
||||
if version.Served {
|
||||
gvs = append(gvs, schema.GroupVersion{Group: crd.Spec.Group, Version: version.Name})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,7 +158,7 @@ func WaitForCRDs(config *rest.Config, crds []client.Object, options CRDInstallOp
|
||||
waitingFor[gv] = &sets.String{}
|
||||
}
|
||||
// Add the Resource
|
||||
waitingFor[gv].Insert(crdPlural)
|
||||
waitingFor[gv].Insert(crd.Spec.Names.Plural)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -263,7 +226,8 @@ func UninstallCRDs(config *rest.Config, options CRDInstallOptions) error {
|
||||
}
|
||||
|
||||
// Uninstall each CRD
|
||||
for _, crd := range runtimeCRDListToUnstructured(options.CRDs) {
|
||||
for _, crd := range options.CRDs {
|
||||
crd := crd
|
||||
log.V(1).Info("uninstalling CRD", "crd", crd.GetName())
|
||||
if err := cs.Delete(context.TODO(), crd); err != nil {
|
||||
// If CRD is not found, we can consider success
|
||||
@@ -277,14 +241,15 @@ func UninstallCRDs(config *rest.Config, options CRDInstallOptions) error {
|
||||
}
|
||||
|
||||
// CreateCRDs creates the CRDs.
|
||||
func CreateCRDs(config *rest.Config, crds []client.Object) error {
|
||||
func CreateCRDs(config *rest.Config, crds []*apiextensionsv1.CustomResourceDefinition) error {
|
||||
cs, err := client.New(config, client.Options{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create client: %w", err)
|
||||
}
|
||||
|
||||
// Create each CRD
|
||||
for _, crd := range runtimeCRDListToUnstructured(crds) {
|
||||
for _, crd := range crds {
|
||||
crd := crd
|
||||
log.V(1).Info("installing CRD", "crd", crd.GetName())
|
||||
existingCrd := crd.DeepCopy()
|
||||
err := cs.Get(context.TODO(), client.ObjectKey{Name: crd.GetName()}, existingCrd)
|
||||
@@ -312,22 +277,21 @@ func CreateCRDs(config *rest.Config, crds []client.Object) error {
|
||||
}
|
||||
|
||||
// renderCRDs iterate through options.Paths and extract all CRD files.
|
||||
func renderCRDs(options *CRDInstallOptions) ([]client.Object, error) {
|
||||
var (
|
||||
err error
|
||||
info os.FileInfo
|
||||
files []os.FileInfo
|
||||
)
|
||||
|
||||
func renderCRDs(options *CRDInstallOptions) ([]*apiextensionsv1.CustomResourceDefinition, error) {
|
||||
type GVKN struct {
|
||||
GVK schema.GroupVersionKind
|
||||
Name string
|
||||
}
|
||||
|
||||
crds := map[GVKN]*unstructured.Unstructured{}
|
||||
crds := map[GVKN]*apiextensionsv1.CustomResourceDefinition{}
|
||||
|
||||
for _, path := range options.Paths {
|
||||
var filePath = path
|
||||
var (
|
||||
err error
|
||||
info os.FileInfo
|
||||
files []string
|
||||
filePath = path
|
||||
)
|
||||
|
||||
// Return the error if ErrorIfPathMissing exists
|
||||
if info, err = os.Stat(path); os.IsNotExist(err) {
|
||||
@@ -338,9 +302,15 @@ func renderCRDs(options *CRDInstallOptions) ([]client.Object, error) {
|
||||
}
|
||||
|
||||
if !info.IsDir() {
|
||||
filePath, files = filepath.Dir(path), []os.FileInfo{info}
|
||||
} else if files, err = ioutil.ReadDir(path); err != nil {
|
||||
return nil, err
|
||||
filePath, files = filepath.Dir(path), []string{info.Name()}
|
||||
} else {
|
||||
entries, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, e := range entries {
|
||||
files = append(files, e.Name())
|
||||
}
|
||||
}
|
||||
|
||||
log.V(1).Info("reading CRDs from path", "path", path)
|
||||
@@ -361,7 +331,7 @@ func renderCRDs(options *CRDInstallOptions) ([]client.Object, error) {
|
||||
}
|
||||
|
||||
// Converting map to a list to return
|
||||
res := []client.Object{}
|
||||
res := []*apiextensionsv1.CustomResourceDefinition{}
|
||||
for _, obj := range crds {
|
||||
res = append(res, obj)
|
||||
}
|
||||
@@ -370,12 +340,7 @@ func renderCRDs(options *CRDInstallOptions) ([]client.Object, error) {
|
||||
|
||||
// modifyConversionWebhooks takes all the registered CustomResourceDefinitions and applies modifications
|
||||
// to conditionally enable webhooks if the type is registered within the scheme.
|
||||
//
|
||||
// The complexity of this function is high mostly due to all the edge cases that we need to handle:
|
||||
// CRDv1beta1, CRDv1, and their unstructured counterpart.
|
||||
//
|
||||
// We should be able to simplify this code once we drop support for v1beta1 and standardize around the typed CRDv1 object.
|
||||
func modifyConversionWebhooks(crds []client.Object, scheme *runtime.Scheme, webhookOptions WebhookInstallOptions) error { //nolint:gocyclo
|
||||
func modifyConversionWebhooks(crds []*apiextensionsv1.CustomResourceDefinition, scheme *runtime.Scheme, webhookOptions WebhookInstallOptions) error {
|
||||
if len(webhookOptions.LocalServingCAData) == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -399,210 +364,74 @@ func modifyConversionWebhooks(crds []client.Object, scheme *runtime.Scheme, webh
|
||||
}
|
||||
url := pointer.StringPtr(fmt.Sprintf("https://%s/convert", hostPort))
|
||||
|
||||
for _, crd := range crds {
|
||||
switch c := crd.(type) {
|
||||
case *apiextensionsv1beta1.CustomResourceDefinition:
|
||||
// Continue if we're preserving unknown fields.
|
||||
//
|
||||
// preserveUnknownFields defaults to true if `nil` in v1beta1.
|
||||
if c.Spec.PreserveUnknownFields == nil || *c.Spec.PreserveUnknownFields {
|
||||
continue
|
||||
}
|
||||
// Continue if the GroupKind isn't registered as being convertible.
|
||||
if _, ok := convertibles[schema.GroupKind{
|
||||
Group: c.Spec.Group,
|
||||
Kind: c.Spec.Names.Kind,
|
||||
}]; !ok {
|
||||
continue
|
||||
}
|
||||
c.Spec.Conversion.Strategy = apiextensionsv1beta1.WebhookConverter
|
||||
c.Spec.Conversion.WebhookClientConfig.Service = nil
|
||||
c.Spec.Conversion.WebhookClientConfig = &apiextensionsv1beta1.WebhookClientConfig{
|
||||
Service: nil,
|
||||
URL: url,
|
||||
CABundle: webhookOptions.LocalServingCAData,
|
||||
}
|
||||
case *apiextensionsv1.CustomResourceDefinition:
|
||||
// Continue if we're preserving unknown fields.
|
||||
if c.Spec.PreserveUnknownFields {
|
||||
continue
|
||||
}
|
||||
// Continue if the GroupKind isn't registered as being convertible.
|
||||
if _, ok := convertibles[schema.GroupKind{
|
||||
Group: c.Spec.Group,
|
||||
Kind: c.Spec.Names.Kind,
|
||||
}]; !ok {
|
||||
continue
|
||||
}
|
||||
c.Spec.Conversion.Strategy = apiextensionsv1.WebhookConverter
|
||||
c.Spec.Conversion.Webhook.ClientConfig.Service = nil
|
||||
c.Spec.Conversion.Webhook.ClientConfig = &apiextensionsv1.WebhookClientConfig{
|
||||
Service: nil,
|
||||
URL: url,
|
||||
CABundle: webhookOptions.LocalServingCAData,
|
||||
}
|
||||
case *unstructured.Unstructured:
|
||||
webhookClientConfig := map[string]interface{}{
|
||||
"url": *url,
|
||||
"caBundle": base64.StdEncoding.EncodeToString(webhookOptions.LocalServingCAData),
|
||||
}
|
||||
|
||||
switch c.GroupVersionKind().Version {
|
||||
case "v1beta1":
|
||||
// Continue if we're preserving unknown fields.
|
||||
//
|
||||
// preserveUnknownFields defaults to true if `nil` in v1beta1.
|
||||
if preserve, found, err := unstructured.NestedBool(c.Object, "spec", "preserveUnknownFields"); preserve || !found {
|
||||
continue
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Continue if the GroupKind isn't registered as being convertible.
|
||||
group, found, err := unstructured.NestedString(c.Object, "spec", "group")
|
||||
if !found {
|
||||
continue
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
kind, found, err := unstructured.NestedString(c.Object, "spec", "names", "kind")
|
||||
if !found {
|
||||
continue
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, ok := convertibles[schema.GroupKind{
|
||||
Group: group,
|
||||
Kind: kind,
|
||||
}]; !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
// Set the strategy.
|
||||
if err := unstructured.SetNestedField(
|
||||
c.Object,
|
||||
string(apiextensionsv1beta1.WebhookConverter),
|
||||
"spec", "conversion", "strategy"); err != nil {
|
||||
return err
|
||||
}
|
||||
// Set the conversion review versions.
|
||||
if err := unstructured.SetNestedStringSlice(
|
||||
c.Object,
|
||||
[]string{"v1beta1"},
|
||||
"spec", "conversion", "webhook", "clientConfig"); err != nil {
|
||||
return err
|
||||
}
|
||||
// Set the client configuration.
|
||||
if err := unstructured.SetNestedMap(
|
||||
c.Object,
|
||||
webhookClientConfig,
|
||||
"spec", "conversion", "webhookClientConfig"); err != nil {
|
||||
return err
|
||||
}
|
||||
case "v1":
|
||||
if preserve, _, err := unstructured.NestedBool(c.Object, "spec", "preserveUnknownFields"); preserve {
|
||||
continue
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Continue if the GroupKind isn't registered as being convertible.
|
||||
group, found, err := unstructured.NestedString(c.Object, "spec", "group")
|
||||
if !found {
|
||||
continue
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
kind, found, err := unstructured.NestedString(c.Object, "spec", "names", "kind")
|
||||
if !found {
|
||||
continue
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, ok := convertibles[schema.GroupKind{
|
||||
Group: group,
|
||||
Kind: kind,
|
||||
}]; !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
// Set the strategy.
|
||||
if err := unstructured.SetNestedField(
|
||||
c.Object,
|
||||
string(apiextensionsv1.WebhookConverter),
|
||||
"spec", "conversion", "strategy"); err != nil {
|
||||
return err
|
||||
}
|
||||
// Set the conversion review versions.
|
||||
if err := unstructured.SetNestedStringSlice(
|
||||
c.Object,
|
||||
[]string{"v1", "v1beta1"},
|
||||
"spec", "conversion", "webhook", "conversionReviewVersions"); err != nil {
|
||||
return err
|
||||
}
|
||||
// Set the client configuration.
|
||||
if err := unstructured.SetNestedMap(
|
||||
c.Object,
|
||||
webhookClientConfig,
|
||||
"spec", "conversion", "webhook", "clientConfig"); err != nil {
|
||||
return err
|
||||
}
|
||||
for i := range crds {
|
||||
// Continue if we're preserving unknown fields.
|
||||
if crds[i].Spec.PreserveUnknownFields {
|
||||
continue
|
||||
}
|
||||
// Continue if the GroupKind isn't registered as being convertible.
|
||||
if _, ok := convertibles[schema.GroupKind{
|
||||
Group: crds[i].Spec.Group,
|
||||
Kind: crds[i].Spec.Names.Kind,
|
||||
}]; !ok {
|
||||
continue
|
||||
}
|
||||
if crds[i].Spec.Conversion == nil {
|
||||
crds[i].Spec.Conversion = &apiextensionsv1.CustomResourceConversion{
|
||||
Webhook: &apiextensionsv1.WebhookConversion{},
|
||||
}
|
||||
}
|
||||
crds[i].Spec.Conversion.Strategy = apiextensionsv1.WebhookConverter
|
||||
crds[i].Spec.Conversion.Webhook.ConversionReviewVersions = []string{"v1", "v1beta1"}
|
||||
crds[i].Spec.Conversion.Webhook.ClientConfig = &apiextensionsv1.WebhookClientConfig{
|
||||
Service: nil,
|
||||
URL: url,
|
||||
CABundle: webhookOptions.LocalServingCAData,
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// readCRDs reads the CRDs from files and Unmarshals them into structs.
|
||||
func readCRDs(basePath string, files []os.FileInfo) ([]*unstructured.Unstructured, error) {
|
||||
var crds []*unstructured.Unstructured
|
||||
func readCRDs(basePath string, files []string) ([]*apiextensionsv1.CustomResourceDefinition, error) {
|
||||
var crds []*apiextensionsv1.CustomResourceDefinition
|
||||
|
||||
// White list the file extensions that may contain CRDs
|
||||
crdExts := sets.NewString(".json", ".yaml", ".yml")
|
||||
|
||||
for _, file := range files {
|
||||
// Only parse allowlisted file types
|
||||
if !crdExts.Has(filepath.Ext(file.Name())) {
|
||||
if !crdExts.Has(filepath.Ext(file)) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Unmarshal CRDs from file into structs
|
||||
docs, err := readDocuments(filepath.Join(basePath, file.Name()))
|
||||
docs, err := readDocuments(filepath.Join(basePath, file))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, doc := range docs {
|
||||
crd := &unstructured.Unstructured{}
|
||||
crd := &apiextensionsv1.CustomResourceDefinition{}
|
||||
if err = yaml.Unmarshal(doc, crd); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Check that it is actually a CRD
|
||||
crdKind, _, err := unstructured.NestedString(crd.Object, "spec", "names", "kind")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
crdGroup, _, err := unstructured.NestedString(crd.Object, "spec", "group")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if crd.GetKind() != "CustomResourceDefinition" || crdKind == "" || crdGroup == "" {
|
||||
if crd.Kind != "CustomResourceDefinition" || crd.Spec.Names.Kind == "" || crd.Spec.Group == "" {
|
||||
continue
|
||||
}
|
||||
crds = append(crds, crd)
|
||||
}
|
||||
|
||||
log.V(1).Info("read CRDs from file", "file", file.Name())
|
||||
log.V(1).Info("read CRDs from file", "file", file)
|
||||
}
|
||||
return crds, nil
|
||||
}
|
||||
|
||||
// readDocuments reads documents from file.
|
||||
func readDocuments(fp string) ([][]byte, error) {
|
||||
b, err := ioutil.ReadFile(fp) //nolint:gosec
|
||||
b, err := os.ReadFile(fp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -613,7 +442,7 @@ func readDocuments(fp string) ([][]byte, error) {
|
||||
// Read document
|
||||
doc, err := reader.Read()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
if errors.Is(err, io.EOF) {
|
||||
break
|
||||
}
|
||||
|
||||
|
||||
33
vendor/sigs.k8s.io/controller-runtime/pkg/envtest/helper.go
generated
vendored
33
vendor/sigs.k8s.io/controller-runtime/pkg/envtest/helper.go
generated
vendored
@@ -18,20 +18,16 @@ package envtest
|
||||
|
||||
import (
|
||||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
||||
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
)
|
||||
|
||||
var (
|
||||
crdScheme = runtime.NewScheme()
|
||||
crdScheme = scheme.Scheme
|
||||
)
|
||||
|
||||
// init is required to correctly initialize the crdScheme package variable.
|
||||
func init() {
|
||||
_ = apiextensionsv1.AddToScheme(crdScheme)
|
||||
_ = apiextensionsv1beta1.AddToScheme(crdScheme)
|
||||
}
|
||||
|
||||
// mergePaths merges two string slices containing paths.
|
||||
@@ -55,32 +51,19 @@ func mergePaths(s1, s2 []string) []string {
|
||||
|
||||
// mergeCRDs merges two CRD slices using their names.
|
||||
// This function makes no guarantees about order of the merged slice.
|
||||
func mergeCRDs(s1, s2 []client.Object) []client.Object {
|
||||
m := make(map[string]*unstructured.Unstructured)
|
||||
for _, obj := range runtimeCRDListToUnstructured(s1) {
|
||||
func mergeCRDs(s1, s2 []*apiextensionsv1.CustomResourceDefinition) []*apiextensionsv1.CustomResourceDefinition {
|
||||
m := make(map[string]*apiextensionsv1.CustomResourceDefinition)
|
||||
for _, obj := range s1 {
|
||||
m[obj.GetName()] = obj
|
||||
}
|
||||
for _, obj := range runtimeCRDListToUnstructured(s2) {
|
||||
for _, obj := range s2 {
|
||||
m[obj.GetName()] = obj
|
||||
}
|
||||
merged := make([]client.Object, len(m))
|
||||
merged := make([]*apiextensionsv1.CustomResourceDefinition, len(m))
|
||||
i := 0
|
||||
for _, obj := range m {
|
||||
merged[i] = obj
|
||||
merged[i] = obj.DeepCopy()
|
||||
i++
|
||||
}
|
||||
return merged
|
||||
}
|
||||
|
||||
func runtimeCRDListToUnstructured(l []client.Object) []*unstructured.Unstructured {
|
||||
res := []*unstructured.Unstructured{}
|
||||
for _, obj := range l {
|
||||
u := &unstructured.Unstructured{}
|
||||
if err := crdScheme.Convert(obj, u, nil); err != nil {
|
||||
log.Error(err, "error converting to unstructured object", "object-kind", obj.GetObjectKind())
|
||||
continue
|
||||
}
|
||||
res = append(res, u)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
8
vendor/sigs.k8s.io/controller-runtime/pkg/envtest/server.go
generated
vendored
8
vendor/sigs.k8s.io/controller-runtime/pkg/envtest/server.go
generated
vendored
@@ -22,15 +22,15 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
"k8s.io/client-go/rest"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/config"
|
||||
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
|
||||
"sigs.k8s.io/controller-runtime/pkg/internal/testing/controlplane"
|
||||
"sigs.k8s.io/controller-runtime/pkg/internal/testing/process"
|
||||
|
||||
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
|
||||
)
|
||||
|
||||
var log = logf.RuntimeLog.WithName("test-env")
|
||||
@@ -136,7 +136,7 @@ type Environment struct {
|
||||
// CRDs is a list of CRDs to install.
|
||||
// If both this field and CRDs field in CRDInstallOptions are specified, the
|
||||
// values are merged.
|
||||
CRDs []client.Object
|
||||
CRDs []*apiextensionsv1.CustomResourceDefinition
|
||||
|
||||
// CRDDirectoryPaths is a list of paths containing CRD yaml or json configs.
|
||||
// If both this field and Paths field in CRDInstallOptions are specified, the
|
||||
|
||||
189
vendor/sigs.k8s.io/controller-runtime/pkg/envtest/webhook.go
generated
vendored
189
vendor/sigs.k8s.io/controller-runtime/pkg/envtest/webhook.go
generated
vendored
@@ -15,26 +15,27 @@ package envtest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
admissionv1 "k8s.io/api/admissionregistration/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
"k8s.io/client-go/rest"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/internal/testing/addr"
|
||||
"sigs.k8s.io/controller-runtime/pkg/internal/testing/certs"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// WebhookInstallOptions are the options for installing mutating or validating webhooks.
|
||||
@@ -43,10 +44,10 @@ type WebhookInstallOptions struct {
|
||||
Paths []string
|
||||
|
||||
// MutatingWebhooks is a list of MutatingWebhookConfigurations to install
|
||||
MutatingWebhooks []client.Object
|
||||
MutatingWebhooks []*admissionv1.MutatingWebhookConfiguration
|
||||
|
||||
// ValidatingWebhooks is a list of ValidatingWebhookConfigurations to install
|
||||
ValidatingWebhooks []client.Object
|
||||
ValidatingWebhooks []*admissionv1.ValidatingWebhookConfiguration
|
||||
|
||||
// IgnoreErrorIfPathMissing will ignore an error if a DirectoryPath does not exist when set to true
|
||||
IgnoreErrorIfPathMissing bool
|
||||
@@ -88,64 +89,34 @@ func (o *WebhookInstallOptions) ModifyWebhookDefinitions() error {
|
||||
return err
|
||||
}
|
||||
|
||||
for i, unstructuredHook := range runtimeListToUnstructured(o.MutatingWebhooks) {
|
||||
webhooks, found, err := unstructured.NestedSlice(unstructuredHook.Object, "webhooks")
|
||||
if !found || err != nil {
|
||||
return fmt.Errorf("unexpected object, %v", err)
|
||||
}
|
||||
for j := range webhooks {
|
||||
webhook, err := modifyWebhook(webhooks[j].(map[string]interface{}), caData, hostPort)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
webhooks[j] = webhook
|
||||
unstructuredHook.Object["webhooks"] = webhooks
|
||||
o.MutatingWebhooks[i] = unstructuredHook
|
||||
for i := range o.MutatingWebhooks {
|
||||
for j := range o.MutatingWebhooks[i].Webhooks {
|
||||
updateClientConfig(&o.MutatingWebhooks[i].Webhooks[j].ClientConfig, hostPort, caData)
|
||||
}
|
||||
}
|
||||
|
||||
for i, unstructuredHook := range runtimeListToUnstructured(o.ValidatingWebhooks) {
|
||||
webhooks, found, err := unstructured.NestedSlice(unstructuredHook.Object, "webhooks")
|
||||
if !found || err != nil {
|
||||
return fmt.Errorf("unexpected object, %v", err)
|
||||
}
|
||||
for j := range webhooks {
|
||||
webhook, err := modifyWebhook(webhooks[j].(map[string]interface{}), caData, hostPort)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
webhooks[j] = webhook
|
||||
unstructuredHook.Object["webhooks"] = webhooks
|
||||
o.ValidatingWebhooks[i] = unstructuredHook
|
||||
for i := range o.ValidatingWebhooks {
|
||||
for j := range o.ValidatingWebhooks[i].Webhooks {
|
||||
updateClientConfig(&o.ValidatingWebhooks[i].Webhooks[j].ClientConfig, hostPort, caData)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func modifyWebhook(webhook map[string]interface{}, caData []byte, hostPort string) (map[string]interface{}, error) {
|
||||
clientConfig, found, err := unstructured.NestedMap(webhook, "clientConfig")
|
||||
if !found || err != nil {
|
||||
return nil, fmt.Errorf("cannot find clientconfig: %v", err)
|
||||
func updateClientConfig(cc *admissionv1.WebhookClientConfig, hostPort string, caData []byte) {
|
||||
cc.CABundle = caData
|
||||
if cc.Service != nil && cc.Service.Path != nil {
|
||||
url := fmt.Sprintf("https://%s/%s", hostPort, *cc.Service.Path)
|
||||
cc.URL = &url
|
||||
cc.Service = nil
|
||||
}
|
||||
clientConfig["caBundle"] = base64.StdEncoding.EncodeToString(caData)
|
||||
servicePath, found, err := unstructured.NestedString(clientConfig, "service", "path")
|
||||
if found && err == nil {
|
||||
// we cannot use service in integration tests since we're running controller outside cluster
|
||||
// the intent here is that we swap out service for raw address because we don't have an actually standard kube service network.
|
||||
// We want to users to be able to use your standard config though
|
||||
url := fmt.Sprintf("https://%s/%s", hostPort, servicePath)
|
||||
clientConfig["url"] = url
|
||||
clientConfig["service"] = nil
|
||||
}
|
||||
webhook["clientConfig"] = clientConfig
|
||||
return webhook, nil
|
||||
}
|
||||
|
||||
func (o *WebhookInstallOptions) generateHostPort() (string, error) {
|
||||
if o.LocalServingPort == 0 {
|
||||
port, host, err := addr.Suggest(o.LocalServingHost)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to grab random port for serving webhooks on: %v", err)
|
||||
return "", fmt.Errorf("unable to grab random port for serving webhooks on: %w", err)
|
||||
}
|
||||
o.LocalServingPort = port
|
||||
o.LocalServingHost = host
|
||||
@@ -199,16 +170,35 @@ func (o *WebhookInstallOptions) Cleanup() error {
|
||||
|
||||
// WaitForWebhooks waits for the Webhooks to be available through API server.
|
||||
func WaitForWebhooks(config *rest.Config,
|
||||
mutatingWebhooks []client.Object,
|
||||
validatingWebhooks []client.Object,
|
||||
mutatingWebhooks []*admissionv1.MutatingWebhookConfiguration,
|
||||
validatingWebhooks []*admissionv1.ValidatingWebhookConfiguration,
|
||||
options WebhookInstallOptions) error {
|
||||
waitingFor := map[schema.GroupVersionKind]*sets.String{}
|
||||
|
||||
for _, hook := range runtimeListToUnstructured(append(validatingWebhooks, mutatingWebhooks...)) {
|
||||
if _, ok := waitingFor[hook.GroupVersionKind()]; !ok {
|
||||
waitingFor[hook.GroupVersionKind()] = &sets.String{}
|
||||
for _, hook := range mutatingWebhooks {
|
||||
h := hook
|
||||
gvk, err := apiutil.GVKForObject(h, scheme.Scheme)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to get gvk for MutatingWebhookConfiguration %s: %w", hook.GetName(), err)
|
||||
}
|
||||
waitingFor[hook.GroupVersionKind()].Insert(hook.GetName())
|
||||
|
||||
if _, ok := waitingFor[gvk]; !ok {
|
||||
waitingFor[gvk] = &sets.String{}
|
||||
}
|
||||
waitingFor[gvk].Insert(h.GetName())
|
||||
}
|
||||
|
||||
for _, hook := range validatingWebhooks {
|
||||
h := hook
|
||||
gvk, err := apiutil.GVKForObject(h, scheme.Scheme)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to get gvk for ValidatingWebhookConfiguration %s: %w", hook.GetName(), err)
|
||||
}
|
||||
|
||||
if _, ok := waitingFor[gvk]; !ok {
|
||||
waitingFor[gvk] = &sets.String{}
|
||||
}
|
||||
waitingFor[gvk].Insert(hook.GetName())
|
||||
}
|
||||
|
||||
// Poll until all resources are found in discovery
|
||||
@@ -266,51 +256,53 @@ func (p *webhookPoller) poll() (done bool, err error) {
|
||||
func (o *WebhookInstallOptions) setupCA() error {
|
||||
hookCA, err := certs.NewTinyCA()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to set up webhook CA: %v", err)
|
||||
return fmt.Errorf("unable to set up webhook CA: %w", err)
|
||||
}
|
||||
|
||||
names := []string{"localhost", o.LocalServingHost, o.LocalServingHostExternalName}
|
||||
hookCert, err := hookCA.NewServingCert(names...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to set up webhook serving certs: %v", err)
|
||||
return fmt.Errorf("unable to set up webhook serving certs: %w", err)
|
||||
}
|
||||
|
||||
localServingCertsDir, err := ioutil.TempDir("", "envtest-serving-certs-")
|
||||
localServingCertsDir, err := os.MkdirTemp("", "envtest-serving-certs-")
|
||||
o.LocalServingCertDir = localServingCertsDir
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create directory for webhook serving certs: %v", err)
|
||||
return fmt.Errorf("unable to create directory for webhook serving certs: %w", err)
|
||||
}
|
||||
|
||||
certData, keyData, err := hookCert.AsBytes()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to marshal webhook serving certs: %v", err)
|
||||
return fmt.Errorf("unable to marshal webhook serving certs: %w", err)
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile(filepath.Join(localServingCertsDir, "tls.crt"), certData, 0640); err != nil { //nolint:gosec
|
||||
return fmt.Errorf("unable to write webhook serving cert to disk: %v", err)
|
||||
if err := os.WriteFile(filepath.Join(localServingCertsDir, "tls.crt"), certData, 0640); err != nil { //nolint:gosec
|
||||
return fmt.Errorf("unable to write webhook serving cert to disk: %w", err)
|
||||
}
|
||||
if err := ioutil.WriteFile(filepath.Join(localServingCertsDir, "tls.key"), keyData, 0640); err != nil { //nolint:gosec
|
||||
return fmt.Errorf("unable to write webhook serving key to disk: %v", err)
|
||||
if err := os.WriteFile(filepath.Join(localServingCertsDir, "tls.key"), keyData, 0640); err != nil { //nolint:gosec
|
||||
return fmt.Errorf("unable to write webhook serving key to disk: %w", err)
|
||||
}
|
||||
|
||||
o.LocalServingCAData = certData
|
||||
return err
|
||||
}
|
||||
|
||||
func createWebhooks(config *rest.Config, mutHooks []client.Object, valHooks []client.Object) error {
|
||||
func createWebhooks(config *rest.Config, mutHooks []*admissionv1.MutatingWebhookConfiguration, valHooks []*admissionv1.ValidatingWebhookConfiguration) error {
|
||||
cs, err := client.New(config, client.Options{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create each webhook
|
||||
for _, hook := range runtimeListToUnstructured(mutHooks) {
|
||||
for _, hook := range mutHooks {
|
||||
hook := hook
|
||||
log.V(1).Info("installing mutating webhook", "webhook", hook.GetName())
|
||||
if err := ensureCreated(cs, hook); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, hook := range runtimeListToUnstructured(valHooks) {
|
||||
for _, hook := range valHooks {
|
||||
hook := hook
|
||||
log.V(1).Info("installing validating webhook", "webhook", hook.GetName())
|
||||
if err := ensureCreated(cs, hook); err != nil {
|
||||
return err
|
||||
@@ -320,8 +312,8 @@ func createWebhooks(config *rest.Config, mutHooks []client.Object, valHooks []cl
|
||||
}
|
||||
|
||||
// ensureCreated creates or update object if already exists in the cluster.
|
||||
func ensureCreated(cs client.Client, obj *unstructured.Unstructured) error {
|
||||
existing := obj.DeepCopy()
|
||||
func ensureCreated(cs client.Client, obj client.Object) error {
|
||||
existing := obj.DeepCopyObject().(client.Object)
|
||||
err := cs.Get(context.Background(), client.ObjectKey{Name: obj.GetName()}, existing)
|
||||
switch {
|
||||
case apierrors.IsNotFound(err):
|
||||
@@ -364,9 +356,9 @@ func parseWebhook(options *WebhookInstallOptions) error {
|
||||
|
||||
// readWebhooks reads the Webhooks from files and Unmarshals them into structs
|
||||
// returns slice of mutating and validating webhook configurations.
|
||||
func readWebhooks(path string) ([]client.Object, []client.Object, error) {
|
||||
func readWebhooks(path string) ([]*admissionv1.MutatingWebhookConfiguration, []*admissionv1.ValidatingWebhookConfiguration, error) {
|
||||
// Get the webhook files
|
||||
var files []os.FileInfo
|
||||
var files []string
|
||||
var err error
|
||||
log.V(1).Info("reading Webhooks from path", "path", path)
|
||||
info, err := os.Stat(path)
|
||||
@@ -374,24 +366,30 @@ func readWebhooks(path string) ([]client.Object, []client.Object, error) {
|
||||
return nil, nil, err
|
||||
}
|
||||
if !info.IsDir() {
|
||||
path, files = filepath.Dir(path), []os.FileInfo{info}
|
||||
} else if files, err = ioutil.ReadDir(path); err != nil {
|
||||
return nil, nil, err
|
||||
path, files = filepath.Dir(path), []string{info.Name()}
|
||||
} else {
|
||||
entries, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
for _, e := range entries {
|
||||
files = append(files, e.Name())
|
||||
}
|
||||
}
|
||||
|
||||
// file extensions that may contain Webhooks
|
||||
resourceExtensions := sets.NewString(".json", ".yaml", ".yml")
|
||||
|
||||
var mutHooks []client.Object
|
||||
var valHooks []client.Object
|
||||
var mutHooks []*admissionv1.MutatingWebhookConfiguration
|
||||
var valHooks []*admissionv1.ValidatingWebhookConfiguration
|
||||
for _, file := range files {
|
||||
// Only parse allowlisted file types
|
||||
if !resourceExtensions.Has(filepath.Ext(file.Name())) {
|
||||
if !resourceExtensions.Has(filepath.Ext(file)) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Unmarshal Webhooks from file into structs
|
||||
docs, err := readDocuments(filepath.Join(path, file.Name()))
|
||||
docs, err := readDocuments(filepath.Join(path, file))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -403,25 +401,24 @@ func readWebhooks(path string) ([]client.Object, []client.Object, error) {
|
||||
}
|
||||
|
||||
const (
|
||||
admissionregv1 = "admissionregistration.k8s.io/v1"
|
||||
admissionregv1beta1 = "admissionregistration.k8s.io/v1beta1"
|
||||
admissionregv1 = "admissionregistration.k8s.io/v1"
|
||||
)
|
||||
switch {
|
||||
case generic.Kind == "MutatingWebhookConfiguration":
|
||||
if generic.APIVersion != admissionregv1beta1 && generic.APIVersion != admissionregv1 {
|
||||
return nil, nil, fmt.Errorf("only v1beta1 and v1 are supported right now for MutatingWebhookConfiguration (name: %s)", generic.Name)
|
||||
if generic.APIVersion != admissionregv1 {
|
||||
return nil, nil, fmt.Errorf("only v1 is supported right now for MutatingWebhookConfiguration (name: %s)", generic.Name)
|
||||
}
|
||||
hook := &unstructured.Unstructured{}
|
||||
if err := yaml.Unmarshal(doc, &hook); err != nil {
|
||||
hook := &admissionv1.MutatingWebhookConfiguration{}
|
||||
if err := yaml.Unmarshal(doc, hook); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
mutHooks = append(mutHooks, hook)
|
||||
case generic.Kind == "ValidatingWebhookConfiguration":
|
||||
if generic.APIVersion != admissionregv1beta1 && generic.APIVersion != admissionregv1 {
|
||||
return nil, nil, fmt.Errorf("only v1beta1 and v1 are supported right now for ValidatingWebhookConfiguration (name: %s)", generic.Name)
|
||||
if generic.APIVersion != admissionregv1 {
|
||||
return nil, nil, fmt.Errorf("only v1 is supported right now for ValidatingWebhookConfiguration (name: %s)", generic.Name)
|
||||
}
|
||||
hook := &unstructured.Unstructured{}
|
||||
if err := yaml.Unmarshal(doc, &hook); err != nil {
|
||||
hook := &admissionv1.ValidatingWebhookConfiguration{}
|
||||
if err := yaml.Unmarshal(doc, hook); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
valHooks = append(valHooks, hook)
|
||||
@@ -430,21 +427,7 @@ func readWebhooks(path string) ([]client.Object, []client.Object, error) {
|
||||
}
|
||||
}
|
||||
|
||||
log.V(1).Info("read webhooks from file", "file", file.Name())
|
||||
log.V(1).Info("read webhooks from file", "file", file)
|
||||
}
|
||||
return mutHooks, valHooks, nil
|
||||
}
|
||||
|
||||
func runtimeListToUnstructured(l []client.Object) []*unstructured.Unstructured {
|
||||
res := []*unstructured.Unstructured{}
|
||||
for _, obj := range l {
|
||||
m, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj.DeepCopyObject())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
res = append(res, &unstructured.Unstructured{
|
||||
Object: m,
|
||||
})
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user