Documentation
¶
Index ¶
- Constants
- func ApplyUnstructuredObjectsWithNewLabels(ctx context.Context, cl client.Client, ...) error
- func CanIssueGitHubRequest(lastGitHubAPICall time.Time) bool
- func CreateTokenRequest(ctx context.Context, restClient *rest.RESTClient, ...) (string, error)
- func EnsureGVK(obj client.Object, scheme *runtime.Scheme) error
- func GetNewConfiguration(newResource client.Object) string
- func MergeAnnotations(toolchainObject client.Object, newAnnotations map[string]string)
- func MergeLabels(toolchainObject client.Object, newLabels map[string]string)
- func NewGitHubClient(ctx context.Context, accessToken string) *github.Client
- func RetainClusterIP(newResource, existing runtime.Object) error
- func SameGVKandName(a, b runtimeclient.Object) bool
- func SortObjectsByName(objects []runtimeclient.Object) []runtimeclient.Object
- type ApplyClient
- func (c ApplyClient) Apply(ctx context.Context, toolchainObjects []client.Object, ...) (bool, error)
- func (c ApplyClient) ApplyObject(ctx context.Context, obj client.Object, options ...ApplyObjectOption) (bool, error)
- func (c ApplyClient) ApplyRuntimeObject(ctx context.Context, obj runtime.Object, options ...ApplyObjectOption) (bool, error)
- type ApplyObjectOption
- type GetGitHubClientFunc
- type GitHubRepository
- type SSAApplyClient
- type SSAApplyObjectOption
Constants ¶
const GitHubAPICallDelay = 1 * time.Minute
GitHubAPICallDelay it's used to "slow" down the number of requests we perform to GitHub API , in order to avoid rate limit issues.
const LastAppliedConfigurationAnnotationKey = "toolchain.dev.openshift.com/last-applied-configuration"
LastAppliedConfigurationAnnotationKey the key to save the last applied configuration in the resource annotations
Variables ¶
This section is empty.
Functions ¶
func ApplyUnstructuredObjectsWithNewLabels ¶
func ApplyUnstructuredObjectsWithNewLabels(ctx context.Context, cl client.Client, unstructuredObjects []*unstructured.Unstructured, newLabels map[string]string) error
ApplyUnstructuredObjectsWithNewLabels applies the given Unstructured objects on the cluster.
func CanIssueGitHubRequest ¶
CanIssueGitHubRequest checks if we already called the GitHub API and if call it again since the preconfigured threshold delay expired.
func CreateTokenRequest ¶
func CreateTokenRequest(ctx context.Context, restClient *rest.RESTClient, namespacedName types.NamespacedName, expirationInSeconds int) (string, error)
CreateTokenRequest creates a TokenRequest for a service account using given expiration in seconds. Returns the token string and nil if everything went fine, otherwise an empty string and an error is returned in case something went wrong.
func EnsureGVK ¶
EnsureGVK makes sure that the object has the GVK set.
If the GVK is empty, it will consult the scheme.
func GetNewConfiguration ¶
func MergeAnnotations ¶
MergeAnnotations gets current exiting annotations and merges them with the new ones provided
func MergeLabels ¶
MergeLabels gets current exiting labels and merges them with the new ones provided
func NewGitHubClient ¶
NewGitHubClient return a client that interacts with GitHub and has rate limiter configured. With authenticated GitHub api you can make 5,000 requests per hour. see: https://github.com/google/go-github#rate-limiting
func RetainClusterIP ¶
RetainClusterIP sets the `spec.clusterIP` value from the given 'existing' object into the 'newResource' object.
func SameGVKandName ¶
func SameGVKandName(a, b runtimeclient.Object) bool
SameGVKandName returns `true` if both objects have the same GroupVersionKind and Name, `false` otherwise
func SortObjectsByName ¶
func SortObjectsByName(objects []runtimeclient.Object) []runtimeclient.Object
SortObjectsByName takes the given list of Objects and sorts them by their namespaced name (it joins the object's namespace and name by a coma and compares them) The resulting sorted array is then returned. This function is important for write predictable and reliable tests
Types ¶
type ApplyClient ¶
ApplyClient the client to use when creating or updating objects
func NewApplyClient ¶
func NewApplyClient(cl client.Client) *ApplyClient
NewApplyClient returns a new ApplyClient
func (ApplyClient) Apply ¶
func (c ApplyClient) Apply(ctx context.Context, toolchainObjects []client.Object, newLabels map[string]string) (bool, error)
Apply applies the objects, ie, creates or updates them on the cluster returns `true, nil` if at least one of the objects was created or modified, `false, nil` if nothing changed, and `false, err` if an error occurred
func (ApplyClient) ApplyObject ¶
func (c ApplyClient) ApplyObject(ctx context.Context, obj client.Object, options ...ApplyObjectOption) (bool, error)
ApplyObject creates the object if is missing and if the owner object is provided, then it's set as a controller reference. If the objects exists then when the spec content has changed (based on the content of the annotation in the original object) then it is automatically updated. If it looks to be same then based on the value of forceUpdate param it updates the object or not. The return boolean says if the object was either created or updated (`true`). If nothing changed (ie, the generation was not incremented by the server), then it returns `false`.
func (ApplyClient) ApplyRuntimeObject ¶
func (c ApplyClient) ApplyRuntimeObject(ctx context.Context, obj runtime.Object, options ...ApplyObjectOption) (bool, error)
ApplyRuntimeObject casts the provided object to client.Object and calls ApplyClient.ApplyObject method
type ApplyObjectOption ¶
type ApplyObjectOption func(*applyObjectConfiguration)
ApplyObjectOption an option when creating or updating a resource
func ForceUpdate ¶
func ForceUpdate(forceUpdate bool) ApplyObjectOption
ForceUpdate forces the update of the resource (default: `false`)
func SaveConfiguration ¶
func SaveConfiguration(saveConfiguration bool) ApplyObjectOption
SaveConfiguration saves the applied configuration in the resource annotations (default: `true`)
func SetOwner ¶
func SetOwner(owner v1.Object) ApplyObjectOption
SetOwner sets the owner of the resource (default: `nil`)
type GetGitHubClientFunc ¶
GetGitHubClientFunc a func that returns a GitHub client instance
type GitHubRepository ¶
type GitHubRepository struct {
Org, Name, Branch, DeployedCommitSHA string
}
type SSAApplyClient ¶
type SSAApplyClient struct {
Client client.Client
// The field owner to use for SSA-applied objects.
FieldOwner string
// MigrateSSAByDefault specifies the default SSA migration behavior.
//
// When checking for the migration, there is an additional GET of the resource, followed by optional
// UPDATE (if the migration is needed) before the actual changes to the objects are applied.
//
// This field specifies the default behavior that can be overridden by supplying an explicit MigrateSSA() option
// to ApplyObject or Apply methods.
//
// The main advantage of using the SSA in our code is that ability of SSA to handle automatic deletion of fields
// that we no longer set in our templates. But this only works when the fields are owned by managers and applied
// using "Apply" operation. As long as there is an "Update" entry with given field (even if the owner is the same)
// the field WILL NOT be automatically deleted by Kubernetes.
//
// Therefore, we need to make sure that our manager uses ONLY the Apply operations. This maximizes the chance
// that the object will look the way we need.
MigrateSSAByDefault bool
// NonSSAFieldOwner should be set to the same value as the user agent used by the provided Kubernetes client
// or to the value of the explicit field owner that the calling code used to use with the normal CRUD operations
// (highly unlikely and not the case in our codebase).
//
// The user agent can be obtained from the REST config from which the client is constructed.
//
// The user agent in the REST config is usually empty, so there's no need to set it here either in that case.
NonSSAFieldOwner string
}
SSAApplyClient the client to use when creating or updating objects. It uses SSA to apply the objects to the cluster.
It doesn't try to migrate the objects from ordinary "CRUD" flow to SSA to be as efficient as possible. If you need to do that check k8s.io/client-go/util/csaupgrade.UpgradeManagedFields().
func NewSSAApplyClient ¶
func NewSSAApplyClient(cl client.Client, fieldOwner string) *SSAApplyClient
NewSSAApplyClient creates a new SSAApplyClient from the provided parameters that will use the provided field owner for the patches.
The returned client checks for the SSA migration by default.
func (*SSAApplyClient) Apply ¶
func (c *SSAApplyClient) Apply(ctx context.Context, toolchainObjects []client.Object, opts ...SSAApplyObjectOption) error
Apply is a utility function that just calls `ApplyObject` in a loop on all the supplied objects.
func (*SSAApplyClient) ApplyObject ¶
func (c *SSAApplyClient) ApplyObject(ctx context.Context, obj client.Object, options ...SSAApplyObjectOption) error
ApplyObject creates the object if is missing or update it if it already exists using an SSA patch.
type SSAApplyObjectOption ¶
type SSAApplyObjectOption func(*ssaApplyObjectConfiguration)
SSAApplyObjectOption an option when creating or updating a resource
func EnsureLabels ¶
func EnsureLabels(labels map[string]string) SSAApplyObjectOption
EnsureLabels makes sure that the provided labels are applied to the object even if the supplied object doesn't have them set.
func MigrateSSA ¶
func MigrateSSA(value bool) SSAApplyObjectOption
MigrateSSA instructs the apply to do the SSA managed fields migration or not. If not used at all, the MigrateSSAByDefault field of the SSA client determines whether the fields will be migrated or not.
func SetOwnerReference ¶
func SetOwnerReference(owner metav1.Object) SSAApplyObjectOption
SetOwnerReference sets the owner reference of the resource (default: `nil`)