...

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

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/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 k8s
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"strings"
    21  
    22  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    23  )
    24  
    25  func GenerateMutableButUnreadableFieldsAnnotation(resource *Resource, mutableButUnreadablePaths [][]string) (string, error) {
    26  	state, err := GenerateMutableButUnreadableFieldsState(resource, mutableButUnreadablePaths)
    27  	if err != nil {
    28  		return "", err
    29  	}
    30  	b, err := json.Marshal(state)
    31  	if err != nil {
    32  		return "", err
    33  	}
    34  	return string(b), nil
    35  }
    36  
    37  func GenerateMutableButUnreadableFieldsState(resource *Resource, mutableButUnreadablePaths [][]string) (map[string]interface{}, error) {
    38  	state := make(map[string]interface{})
    39  	for _, path := range mutableButUnreadablePaths {
    40  		krmPath := append([]string{"spec"}, path...)
    41  		krmField := strings.Join(krmPath, ".")
    42  		val, found, err := unstructured.NestedFieldCopy(resource.Spec, krmPath[1:]...)
    43  		if err != nil {
    44  			return nil, fmt.Errorf("error reading %v: %v", krmField, err)
    45  		}
    46  		if !found {
    47  			continue
    48  		}
    49  		if err := unstructured.SetNestedField(state, val, krmPath...); err != nil {
    50  			return nil, fmt.Errorf("error saving %v: %v", krmField, err)
    51  		}
    52  	}
    53  	return state, nil
    54  }
    55  
    56  func GetMutableButUnreadableFieldsFromAnnotations(resource *Resource, mutableButUnreadablePaths [][]string) (map[string]interface{}, error) {
    57  	mutableButUnreadableFields := make(map[string]interface{})
    58  	if len(mutableButUnreadablePaths) == 0 {
    59  		// The resource does not have any mutable-but-unreadable field.
    60  		return mutableButUnreadableFields, nil
    61  	}
    62  
    63  	var err error
    64  	annotationVal, ok := GetAnnotation(MutableButUnreadableFieldsAnnotation, resource)
    65  	if !ok {
    66  		// If the resource can have mutable-but-unreadable fields but does not
    67  		// have the annotation set at all, then this is either (1) a new resource,
    68  		// (2) a resource acquisition, or (3) a resource created before it supported
    69  		// mutable-but-unreadable fields.
    70  		// To avoid unnecessarily updating the resource in Cases 2 and 3, generate
    71  		// a value for the annotation based on its current spec.
    72  		// Note that while this will also generate an annotation for Case 1
    73  		// (ideally it shouldn't as the resource technically doesn't exist yet),
    74  		// the controller will actually end up ignoring it since the controller
    75  		// will force a resource creation once it detects that the underlying
    76  		// resource doesn't exist.
    77  		annotationVal, err = GenerateMutableButUnreadableFieldsAnnotation(resource, mutableButUnreadablePaths)
    78  		if err != nil {
    79  			return nil, fmt.Errorf("error ensuring resource '%v' which can have mutable-but-unreadable fields has a value for %v: %w",
    80  				resource.GetNamespace(), MutableButUnreadableFieldsAnnotation, err)
    81  		}
    82  	}
    83  
    84  	if err := json.Unmarshal([]byte(annotationVal), &mutableButUnreadableFields); err != nil {
    85  		return nil, fmt.Errorf("error unmarshalling value of %v: %w", MutableButUnreadableFieldsAnnotation, err)
    86  	}
    87  	return mutableButUnreadableFields, nil
    88  }
    89  

View as plain text