client

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 10, 2024 License: Apache-2.0 Imports: 15 Imported by: 1

Documentation

Index

Examples

Constants

View Source
const (
	DbUsernameName = "USERNAME"
	DbPasswordName = "PASSWORD"
)

Variables

This section is empty.

Functions

func GetObjectGVK

func GetObjectGVK(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.

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) 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 a context and an object that implements the `ctrlclient.Object` interface. The function returns a boolean indicating whether a mutation occurred and an error, if any. If the object does not exist, a new one is created. If the object already exists, it is updated. The function first checks if the object exists by calling `Get` on the client. If the object does not exist, it creates a new one by calling `Create` on the client. If the object exists, it calculates the patch to match the current and desired objects using `patch.DefaultPatchMaker.Calculate`. If the patch is not empty, it updates the object by calling `Update` on the client. The function also handles specific logic for `Service` and `StatefulSet` objects. For `Service` objects, it preserves the `ClusterIP` and annotations when updating. For `StatefulSet` objects, it ignores the `VolumeClaimTemplate` type metadata and status when calculating the patch. The function sets the `LastAppliedAnnotation` on the object using `patch.DefaultAnnotator.SetLastAppliedAnnotation`. It also sets the `ResourceVersion` of the object to match the current object before updating. The function returns `true` if a mutation occurred, indicating that the object was created or updated. If no mutation occurred, it returns `false`. If an error occurs during any step, the function returns the error. 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, obj ctrlclient.Object) error

Get retrieves the object specified by the given `obj` from the Kubernetes cluster. It accepts a context `ctx` for cancellation and a `obj` of type `ctrlclient.Object` that represents the object to be retrieved. If the `obj` does not have a namespace specified, it uses the owner's namespace. It returns an error if the retrieval fails, along with additional information about the resource. Parameters:

  • ctx: The context for the operation.
  • obj: The object to retrieve.

Returns:

  • error: An error if the operation fails, otherwise nil.
Example
client := &Client{}

// Get a service in the same namespace as the owner object
svcInOwnerNamespace := &corev1.Service{
	ObjectMeta: metav1.ObjectMeta{Name: "my-svc"},
}

if err := client.Get(context.Background(), svcInOwnerNamespace); err != nil {
	return
}

// Get a service in another namespace
svcInAnotherNamespace := &corev1.Service{
	ObjectMeta: metav1.ObjectMeta{Name: "my-svc", Namespace: "another-ns"},
}
if err := client.Get(context.Background(), svcInAnotherNamespace); err != nil {
	return
}

func (*Client) GetCtrlClient

func (c *Client) GetCtrlClient() ctrlclient.Client

GetCtrlClient returns the control client associated with the client.

func (*Client) GetCtrlScheme

func (c *Client) GetCtrlScheme() *runtime.Scheme

GetCtrlScheme returns the control scheme used by the client.

func (*Client) GetOwnerName

func (c *Client) GetOwnerName() string

GetOwnerName returns the name of the owner reference.

func (*Client) GetOwnerNamespace

func (c *Client) GetOwnerNamespace() string

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) 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.

type DataBaseType

type DataBaseType string
const (
	Postgres DataBaseType = "postgresql"
	Mysql    DataBaseType = "mysql"
	Derby    DataBaseType = "derby"
	Unknown  DataBaseType = "unknown"
)

type DatabaseConfiguration

type DatabaseConfiguration struct {
	DbReference *string
	DbInline    *DatabaseParams
	Namespace   string
	Context     context.Context
	Client      *Client
}

DatabaseConfiguration is a struct that holds the configuration for a database. example1:

dbConfig := &DatabaseConfiguration{DbReference: &ref, Context: ctx, Client: client}
dbConfig.GetDatabaseParams()
dbConfig.GetURI()

example2:

func (*DatabaseConfiguration) GetCredential

func (d *DatabaseConfiguration) GetCredential(name string) (*DatabaseCredential, error)

func (*DatabaseConfiguration) GetDatabaseParams

func (d *DatabaseConfiguration) GetDatabaseParams() (*DatabaseParams, error)

func (*DatabaseConfiguration) GetJDBCUrl

func (d *DatabaseConfiguration) GetJDBCUrl() (string, error)

GetJDBCUrl returns the JDBC URL for the database. Supported: - Postgres - Mysql - Derby

  • `derby:dbName;create=true`, the dbName is a file path.

func (*DatabaseConfiguration) GetNamespace

func (d *DatabaseConfiguration) GetNamespace() string

func (*DatabaseConfiguration) GetRefDatabase

func (d *DatabaseConfiguration) GetRefDatabase(ctx context.Context) (dbv1alpha1.Database, error)

func (*DatabaseConfiguration) GetRefDatabaseConnection

func (d *DatabaseConfiguration) GetRefDatabaseConnection(name string) (dbv1alpha1.DatabaseConnection, error)

func (*DatabaseConfiguration) GetRefDatabaseName

func (d *DatabaseConfiguration) GetRefDatabaseName() string

type DatabaseCredential

type DatabaseCredential struct {
	Username string `json:"USERNAME"`
	Password string `json:"PASSWORD"`
}

type DatabaseParams

type DatabaseParams struct {
	DbType   DataBaseType
	Driver   string
	Username string
	Password string
	Host     string
	Port     int32
	DbName   string
}

func NewDatabaseParams

func NewDatabaseParams(
	Driver string,
	Username string,
	Password string,
	Host string,
	Port int32,
	DbName string) *DatabaseParams

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL