Documentation
¶
Index ¶
- func CreateDoesNotExist(ctx context.Context, client ctrlclient.Client, obj ctrlclient.Object) error
- func CreateOrUpdate(ctx context.Context, client ctrlclient.Client, obj ctrlclient.Object) (mutation bool, err error)
- func GetObjectGVK(schema *runtime.Scheme, obj ctrlclient.Object) (*schema.GroupVersionKind, error)
- func UpdateRetryOnConflict(ctx context.Context, client ctrlclient.Client, obj ctrlclient.Object) error
- type Client
- func (c *Client) CreateDoesNotExist(ctx context.Context, obj ctrlclient.Object) error
- func (c *Client) CreateOrUpdate(ctx context.Context, obj ctrlclient.Object) (mutation bool, err error)
- func (c *Client) Get(ctx context.Context, key ctrlclient.ObjectKey, obj ctrlclient.Object, ...) error
- func (c *Client) GetCtrlClient() ctrlclient.Client
- func (c *Client) GetCtrlScheme() *runtime.Scheme
- func (c *Client) GetOwnerName() string
- func (c *Client) GetOwnerNamespace() string
- func (c *Client) GetOwnerReference() ctrlclient.Object
- func (c *Client) GetWithObject(ctx context.Context, obj ctrlclient.Object, opts ...ctrlclient.GetOption) error
- func (c *Client) GetWithOwnerNamespace(ctx context.Context, name string, obj ctrlclient.Object, ...) error
- func (c *Client) SetOwnerReference(obj ctrlclient.Object, gvk *schema.GroupVersionKind) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateDoesNotExist ¶ added in v0.12.0
func CreateDoesNotExist(ctx context.Context, client ctrlclient.Client, obj ctrlclient.Object) error
CreateDoesNotExist attempts to create a Kubernetes resource. If the resource does not exist, it creates it; if it already exists, it skips. Parameters:
- ctx: The context for the operation.
- client: The Kubernetes client used to interact with the cluster.
- obj: The resource object to create.
Returns:
- error: Returns an error if the operation fails, otherwise nil.
func CreateOrUpdate ¶ added in v0.8.1
func CreateOrUpdate(ctx context.Context, client ctrlclient.Client, obj ctrlclient.Object) (mutation bool, err error)
CreateOrUpdate creates or updates an object in the Kubernetes cluster. It takes the following parameters: - ctx: The context.Context object for the operation. - obj: The object to be created or updated. - client: The Kubernetes client used to interact with the cluster. It returns a boolean value indicating whether the object was mutated and an error, if any. The function first checks if the object exists in the cluster. If it doesn't, a new object is created. If the object already exists, it calculates the patch to match the existing object and the desired object. If the patch is not empty, it updates the object with the patch. The function also preserves certain fields and annotations during the update process. If any error occurs during the creation or update, it is returned along with the mutation status. Parameters:
- ctx: The context for the operation.
- client: The Kubernetes client used to interact with the cluster.
- obj: The object to create or update.
Returns:
- mutation: A boolean indicating whether a resource changed.
- error: An error if the operation fails, otherwise nil.
func GetObjectGVK ¶
func GetObjectGVK(schema *runtime.Scheme, obj ctrlclient.Object) (*schema.GroupVersionKind, error)
GetObjectGVK returns the GroupVersionKind (GVK) of the provided object. It retrieves the GVK by using the scheme.Scheme.ObjectKinds function. If the GVK is not found or there is an error retrieving it, an error is returned.
func UpdateRetryOnConflict ¶ added in v0.12.4
func UpdateRetryOnConflict(ctx context.Context, client ctrlclient.Client, obj ctrlclient.Object) error
UpdateRetryOnConflict attempts to update a Kubernetes resource. It uses a retry mechanism to handle conflicts during the update process. If the update is successful, it logs a success message. If the update fails, it logs an error message. Parameters:
- ctx: The context for the operation.
- client: The Kubernetes client used to interact with the cluster.
- obj: The resource object to update.
Returns:
- error: Returns an error if the operation fails, otherwise nil.
Types ¶
type Client ¶
type Client struct { Client ctrlclient.Client OwnerReference ctrlclient.Object }
func NewClient ¶ added in v0.8.0
func NewClient(client ctrlclient.Client, ownerReference ctrlclient.Object) *Client
NewClient returns a new instance of the Client struct. It accepts a control client `client` and an owner reference `ownerReference`.
func (*Client) CreateDoesNotExist ¶ added in v0.12.0
func (*Client) CreateOrUpdate ¶
func (c *Client) CreateOrUpdate(ctx context.Context, obj ctrlclient.Object) (mutation bool, err error)
CreateOrUpdate creates or updates an object in the Kubernetes cluster. It takes the following parameters: - ctx: The context.Context object for the operation. - obj: The object to be created or updated. - client: The Kubernetes client used to interact with the cluster. It returns a boolean value indicating whether the object was mutated and an error, if any. The function first checks if the object exists in the cluster. If it doesn't, a new object is created. If the object already exists, it calculates the patch to match the existing object and the desired object. If the patch is not empty, it updates the object with the patch. The function also preserves certain fields and annotations during the update process. If any error occurs during the creation or update, it is returned along with the mutation status. Parameters:
- ctx: The context for the operation.
- obj: The object to create or update.
Returns:
- mutation: A boolean indicating whether a mutation occurred.
- error: An error if the operation fails, otherwise nil.
func (*Client) Get ¶
func (c *Client) Get(ctx context.Context, key ctrlclient.ObjectKey, obj ctrlclient.Object, opts ...ctrlclient.GetOption) error
Get wrapped Get method for the client, and provides logging for the operation when it fails. Parameters:
- ctx: The context for the operation.
- key: The key of the object to retrieve.
- obj: The object to retrieve.
- opts: Additional options for the operation.
Returns:
- error: An error if the operation fails, otherwise nil.
func (*Client) GetCtrlClient ¶
func (c *Client) GetCtrlClient() ctrlclient.Client
GetCtrlClient returns the control client associated with the client.
func (*Client) GetCtrlScheme ¶
GetCtrlScheme returns the control scheme used by the client.
func (*Client) GetOwnerName ¶
GetOwnerName returns the name of the owner reference.
func (*Client) GetOwnerNamespace ¶
GetOwnerNamespace returns the namespace of the owner reference.
func (*Client) GetOwnerReference ¶
func (c *Client) GetOwnerReference() ctrlclient.Object
GetOwnerReference returns the owner reference of the client.
func (*Client) GetWithObject ¶ added in v0.10.0
func (c *Client) GetWithObject(ctx context.Context, obj ctrlclient.Object, opts ...ctrlclient.GetOption) error
func (*Client) GetWithOwnerNamespace ¶ added in v0.10.0
func (c *Client) GetWithOwnerNamespace(ctx context.Context, name string, obj ctrlclient.Object, opts ...ctrlclient.GetOption) error
func (*Client) SetOwnerReference ¶
func (c *Client) SetOwnerReference(obj ctrlclient.Object, gvk *schema.GroupVersionKind) error
SetOwnerReference sets the owner reference for the given object. If the object doesn't have a namespace, it skips setting the owner reference and returns nil. If the client's OwnerReference is nil, it skips setting the owner reference and returns nil. Otherwise, it sets the owner reference using the ctrl.SetControllerReference function. It logs an error if setting the owner reference fails. Finally, it logs a message indicating that the owner reference has been set for the object. Parameters:
- obj: The object for which to set the owner reference.
- gvk: The GroupVersionKind of the object.
Returns:
- error: An error if setting the owner reference fails, otherwise nil.