...

Source file src/k8s.io/apimachinery/pkg/util/managedfields/managedfieldstest/testfieldmanager.go

Documentation: k8s.io/apimachinery/pkg/util/managedfields/managedfieldstest

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package managedfieldstest
    18  
    19  import (
    20  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    21  	"k8s.io/apimachinery/pkg/runtime"
    22  	"k8s.io/apimachinery/pkg/runtime/schema"
    23  	"k8s.io/apimachinery/pkg/util/managedfields"
    24  	"k8s.io/apimachinery/pkg/util/managedfields/internal/testing"
    25  )
    26  
    27  // TestFieldManager is a FieldManager that can be used in test to
    28  // simulate the behavior of Server-Side Apply and field tracking. This
    29  // also has a few methods to get a sense of the state of the object.
    30  //
    31  // This TestFieldManager uses a series of "fake" objects to simulate
    32  // some behavior which come with the limitation that you can only use
    33  // one version since there is no version conversion logic.
    34  //
    35  // You can use this rather than NewDefaultTestFieldManager if you want
    36  // to specify either a sub-resource, or a set of modified Manager to
    37  // test them specifically.
    38  type TestFieldManager interface {
    39  	// APIVersion of the object that we're tracking.
    40  	APIVersion() string
    41  	// Reset resets the state of the liveObject by resetting it to an empty object.
    42  	Reset()
    43  	// Live returns a copy of the current liveObject.
    44  	Live() runtime.Object
    45  	// Apply applies the given object on top of the current liveObj, for the
    46  	// given manager and force flag.
    47  	Apply(obj runtime.Object, manager string, force bool) error
    48  	// Update will updates the managed fields in the liveObj based on the
    49  	// changes performed by the update.
    50  	Update(obj runtime.Object, manager string) error
    51  	// ManagedFields returns the list of existing managed fields for the
    52  	// liveObj.
    53  	ManagedFields() []metav1.ManagedFieldsEntry
    54  }
    55  
    56  // NewTestFieldManager returns a new TestFieldManager built for the
    57  // given gvk, on the main resource.
    58  func NewTestFieldManager(typeConverter managedfields.TypeConverter, gvk schema.GroupVersionKind) TestFieldManager {
    59  	return testing.NewTestFieldManagerImpl(typeConverter, gvk, "", nil)
    60  }
    61  
    62  // NewTestFieldManagerSubresource returns a new TestFieldManager built
    63  // for the given gvk, on the given sub-resource.
    64  func NewTestFieldManagerSubresource(typeConverter managedfields.TypeConverter, gvk schema.GroupVersionKind, subresource string) TestFieldManager {
    65  	return testing.NewTestFieldManagerImpl(typeConverter, gvk, subresource, nil)
    66  
    67  }
    68  
    69  // NewFakeFieldManager creates an actual FieldManager but that doesn't
    70  // perform any conversion. This is just a convenience for tests to
    71  // create an actual manager that they can use but in very restricted
    72  // ways.
    73  //
    74  // This is different from the TestFieldManager because it's not meant to
    75  // assert values, or hold the state, this acts like a normal
    76  // FieldManager.
    77  //
    78  // Also, this only operates on the main-resource, and sub-resource can't
    79  // be configured.
    80  func NewFakeFieldManager(typeConverter managedfields.TypeConverter, gvk schema.GroupVersionKind) *managedfields.FieldManager {
    81  	ffm, err := managedfields.NewDefaultFieldManager(
    82  		typeConverter,
    83  		&testing.FakeObjectConvertor{},
    84  		&testing.FakeObjectDefaulter{},
    85  		&testing.FakeObjectCreater{},
    86  		gvk,
    87  		gvk.GroupVersion(),
    88  		"",
    89  		nil)
    90  	if err != nil {
    91  		panic(err)
    92  	}
    93  	return ffm
    94  }
    95  

View as plain text