provider

package
v0.51.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2025 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// labels
	LabelManagedBy          = "pipecd.dev/managed-by"            // Always be piped.
	LabelPiped              = "pipecd.dev/piped"                 // The id of piped handling this application.
	LabelApplication        = "pipecd.dev/application"           // The application this resource belongs to.
	LabelSyncReplace        = "pipecd.dev/sync-by-replace"       // Use replace instead of apply.
	LabelForceSyncReplace   = "pipecd.dev/force-sync-by-replace" // Use replace --force instead of apply.
	LabelServerSideApply    = "pipecd.dev/server-side-apply"     // Use server side apply instead of client side apply.
	LabelCommitHash         = "pipecd.dev/commit-hash"           // Hash value of the deployed commit.
	LabelResourceKey        = "pipecd.dev/resource-key"          // The resource key generated by apiVersion, namespace and name. e.g. apps/v1/Deployment/namespace/demo-app
	LabelOriginalAPIVersion = "pipecd.dev/original-api-version"  // The api version defined in git configuration. e.g. apps/v1

	// annotations
	AnnotationOrder      = "pipecd.dev/order"       // The order number of resource used to sort them before using.
	AnnotationConfigHash = "pipecd.dev/config-hash" // The hash value of all mouting config resources.

	// label/annotation values
	ManagedByPiped     = "piped"
	UseReplaceEnabled  = "enabled"
	UseServerSideApply = "true"
)
View Source
const (
	KindDeployment = "Deployment"
	KindSecret     = "Secret"
	KindConfigMap  = "ConfigMap"

	DefaultNamespace = "default"
)

Variables

View Source
var (
	ErrNotFound = errors.New("not found")
)

Functions

func BuildApplicationLiveState added in v0.51.0

func BuildApplicationLiveState(deploytarget string, manifests []Manifest, now time.Time) *model.ApplicationLiveState

BuildApplicationLiveState builds the live state of the application from the given manifests.

func Diff

func Diff(old, new Manifest, logger *zap.Logger, opts ...diff.Option) (*diff.Result, error)

func FindConfigsAndSecrets added in v0.51.0

func FindConfigsAndSecrets(manifests []Manifest) map[ResourceKey]Manifest

FindConfigsAndSecrets returns the manifests that are ConfigMap or Secret.

func FindContainerImages added in v0.51.0

func FindContainerImages(m Manifest) []string

FindContainerImages finds all container images that are referenced by the given manifest.

It looks for container images in the following fields: - spec.template.spec.containers.image

TODO: we should consider other fields like spec.template.spec.initContainers.image, spec.jobTempate.spec.template.spec.containers.image

func FindReferencingConfigMaps added in v0.51.0

func FindReferencingConfigMaps(m Manifest) []string

FindReferencingConfigMaps finds all configmaps that are referenced by the given manifest.

It looks for configmaps in the following fields: - spec.template.spec.volumes.configMap.name - spec.template.spec.initContainers.env.valueFrom.configMapKeyRef.name - spec.template.spec.initContainers.envFrom.configMapRef.name - spec.template.spec.containers.env.valueFrom.configMapKeyRef.name - spec.template.spec.containers.envFrom.configMapRef.name

func FindReferencingSecrets added in v0.51.0

func FindReferencingSecrets(m Manifest) []string

FindReferencingSecrets finds all secrets that are referenced by the given manifest.

It looks for secrets in the following fields: - spec.template.spec.volumes.secret.secretName - spec.template.spec.initContainers.env.valueFrom.secretKeyRef.name - spec.template.spec.initContainers.envFrom.secretRef.name - spec.template.spec.containers.env.valueFrom.secretKeyRef.name - spec.template.spec.containers.envFrom.secretRef.name

func GetLiveResources added in v0.51.0

func GetLiveResources(ctx context.Context, kubectl *Kubectl, kubeconfig string, appID string, selector ...string) (namespaceScoped []Manifest, clusterScoped []Manifest, _ error)

GetLiveResources returns all live resources that belong to the given application.

func HashManifests added in v0.51.0

func HashManifests(manifests []Manifest) (string, error)

HashManifests computes the hash of a list of manifests.

Types

type Applier added in v0.51.0

type Applier struct {
	// contains filtered or unexported fields
}

func NewApplier added in v0.51.0

func NewApplier(kubectl kubectl, input config.KubernetesDeploymentInput, cp config.KubernetesDeployTargetConfig, logger *zap.Logger) *Applier

func (*Applier) ApplyManifest added in v0.51.0

func (a *Applier) ApplyManifest(ctx context.Context, manifest Manifest) error

ApplyManifest does applying the given manifest.

func (*Applier) CreateManifest added in v0.51.0

func (a *Applier) CreateManifest(ctx context.Context, manifest Manifest) error

CreateManifest uses kubectl to create the given manifests.

func (*Applier) Delete added in v0.51.0

func (a *Applier) Delete(ctx context.Context, k ResourceKey) (err error)

Delete deletes the given resource from Kubernetes cluster. If the resource key is different, this returns ErrNotFound.

func (*Applier) ForceReplaceManifest added in v0.51.0

func (a *Applier) ForceReplaceManifest(ctx context.Context, manifest Manifest) error

ForceReplaceManifest uses kubectl to forcefully replace the given manifests.

func (*Applier) ReplaceManifest added in v0.51.0

func (a *Applier) ReplaceManifest(ctx context.Context, manifest Manifest) error

ReplaceManifest uses kubectl to replace the given manifests.

type DiffListChange added in v0.51.0

type DiffListChange struct {
	Old  Manifest
	New  Manifest
	Diff *diff.Result
}

type DiffListResult added in v0.51.0

type DiffListResult struct {
	Adds    []Manifest
	Deletes []Manifest
	Changes []DiffListChange
}

func DiffList added in v0.51.0

func DiffList(liveManifests, desiredManifests []Manifest, logger *zap.Logger, opts ...diff.Option) (*DiffListResult, error)

func (*DiffListResult) NoChanges added in v0.51.0

func (r *DiffListResult) NoChanges() bool

func (*DiffListResult) Render added in v0.51.0

func (r *DiffListResult) Render(opt DiffRenderOptions) string

func (*DiffListResult) TotalOutOfSync added in v0.51.0

func (r *DiffListResult) TotalOutOfSync() int

type DiffRenderOptions added in v0.51.0

type DiffRenderOptions struct {
	MaskSecret    bool
	MaskConfigMap bool
	// Maximum number of changed manifests should be shown.
	// Zero means rendering all.
	MaxChangedManifests int
}

type Helm

type Helm struct {
	// contains filtered or unexported fields
}

func NewHelm

func NewHelm(version, path string, logger *zap.Logger) *Helm

func (*Helm) TemplateLocalChart

func (h *Helm) TemplateLocalChart(ctx context.Context, appName, appDir, namespace, chartPath string, opts *config.InputHelmOptions) (string, error)

type Kubectl added in v0.51.0

type Kubectl struct {
	// contains filtered or unexported fields
}

Kubectl is a wrapper for kubectl command.

func NewKubectl added in v0.51.0

func NewKubectl(path string) *Kubectl

NewKubectl creates a new Kubectl instance.

func (*Kubectl) Apply added in v0.51.0

func (c *Kubectl) Apply(ctx context.Context, kubeconfig, namespace string, manifest Manifest) (err error)

Apply runs kubectl apply command with the given manifest.

func (*Kubectl) Create added in v0.51.0

func (c *Kubectl) Create(ctx context.Context, kubeconfig, namespace string, manifest Manifest) (err error)

Create runs kubectl create command with the given manifest.

func (*Kubectl) CreateNamespace added in v0.51.0

func (c *Kubectl) CreateNamespace(ctx context.Context, kubeconfig, namespace string) (err error)

CreateNamespace runs kubectl create namespace with the given namespace.

func (*Kubectl) Delete added in v0.51.0

func (c *Kubectl) Delete(ctx context.Context, kubeconfig, namespace string, r ResourceKey) (err error)

Delete runs kubectl delete command with the given resource key.

func (*Kubectl) ForceReplace added in v0.51.0

func (c *Kubectl) ForceReplace(ctx context.Context, kubeconfig, namespace string, manifest Manifest) (err error)

ForceReplace runs kubectl replace --force command with the given manifest.

func (*Kubectl) Get added in v0.51.0

func (c *Kubectl) Get(ctx context.Context, kubeconfig, namespace string, r ResourceKey) (m Manifest, err error)

Get runs kubectl get with the gibven resource key.

func (*Kubectl) GetAll added in v0.51.0

func (c *Kubectl) GetAll(ctx context.Context, kubeconfig, namespace string, selector ...string) (ms []Manifest, err error)

GetAll retrieves all Kubernetes resources in the specified namespace and matching the given selector. It returns a list of manifests or an error if the retrieval or unmarshalling fails. If no resources are found, it returns nil without an error.

func (*Kubectl) GetAllClusterScoped added in v0.51.0

func (c *Kubectl) GetAllClusterScoped(ctx context.Context, kubeconfig string, selector ...string) ([]Manifest, error)

GetAllClusterScoped retrieves all cluster-scoped resources from the Kubernetes cluster using the provided kubeconfig and optional selectors. It returns a slice of Manifests representing the resources or an error if the operation fails.

func (*Kubectl) Replace added in v0.51.0

func (c *Kubectl) Replace(ctx context.Context, kubeconfig, namespace string, manifest Manifest) (err error)

Replace runs kubectl replace command with the given manifest.

type Kustomize

type Kustomize struct {
	// contains filtered or unexported fields
}

func NewKustomize

func NewKustomize(version, path string, logger *zap.Logger) *Kustomize

func (*Kustomize) Template

func (c *Kustomize) Template(ctx context.Context, appName, appDir string, opts map[string]string, helm *Helm) (string, error)

type Loader

type Loader struct {
	// contains filtered or unexported fields
}

func NewLoader added in v0.51.0

func NewLoader(registry ToolRegistry) *Loader

func (*Loader) LoadManifests

func (l *Loader) LoadManifests(ctx context.Context, input LoaderInput) (manifests []Manifest, err error)

type LoaderInput

type LoaderInput struct {
	// for annotations to manage the application live state.
	PipedID    string
	CommitHash string
	AppID      string

	// for templating manifests
	AppName        string
	AppDir         string
	ConfigFilename string
	Manifests      []string

	Namespace        string
	TemplatingMethod TemplatingMethod

	KustomizeVersion string
	KustomizeOptions map[string]string

	HelmVersion string
	HelmChart   *config.InputHelmChart
	HelmOptions *config.InputHelmOptions

	Logger *zap.Logger
}

type Manifest

type Manifest struct {
	// contains filtered or unexported fields
}

Manifest represents a Kubernetes resource manifest.

func LoadManifestsFromYAMLFile

func LoadManifestsFromYAMLFile(path string) ([]Manifest, error)

LoadManifestsFromYAMLFile loads the manifests from the given file.

func LoadPlainYAMLManifests

func LoadPlainYAMLManifests(dir string, names []string, configFilename string) ([]Manifest, error)

func ParseManifests

func ParseManifests(data string) ([]Manifest, error)

ParseManifests parses the given data and returns a list of Manifest.

func (Manifest) AddAnnotations

func (m Manifest) AddAnnotations(annotations map[string]string)

func (Manifest) AddLabels added in v0.51.0

func (m Manifest) AddLabels(labels map[string]string)

func (Manifest) AddStringMapValues added in v0.51.0

func (m Manifest) AddStringMapValues(values map[string]string, fields ...string) error

AddStringMapValues adds or overrides the given key-values into the string map that can be found at the specified fields.

func (Manifest) ConvertToStructuredObject added in v0.51.0

func (m Manifest) ConvertToStructuredObject(o interface{}) error

ConvertToStructuredObject converts the manifest into a structured Kubernetes object. The provided interface should be a pointer to a concrete Kubernetes type (e.g. *v1.Pod). It first marshals the manifest to JSON and then unmarshals it into the provided object.

func (Manifest) GetAnnotations added in v0.51.0

func (m Manifest) GetAnnotations() map[string]string

func (Manifest) IsConfigMap added in v0.51.0

func (m Manifest) IsConfigMap() bool

IsConfigMap returns true if the manifest is a ConfigMap. It checks the API group and the kind of the manifest.

func (Manifest) IsDeployment added in v0.51.0

func (m Manifest) IsDeployment() bool

IsDeployment returns true if the manifest is a Deployment. It checks the API group and the kind of the manifest.

func (Manifest) IsManagedByPiped added in v0.51.0

func (m Manifest) IsManagedByPiped() bool

IsManagedByPiped returns true if the manifest is managed by Piped.

func (Manifest) IsSecret added in v0.51.0

func (m Manifest) IsSecret() bool

IsSecret returns true if the manifest is a Secret. It checks the API group and the kind of the manifest.

func (Manifest) Key

func (m Manifest) Key() ResourceKey

func (Manifest) Kind added in v0.51.0

func (m Manifest) Kind() string

func (*Manifest) MarshalJSON added in v0.51.0

func (m *Manifest) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. It marshals the underlying unstructured.Unstructured object into JSON bytes.

func (Manifest) Name added in v0.51.0

func (m Manifest) Name() string

func (Manifest) NestedMap added in v0.51.0

func (m Manifest) NestedMap(fields ...string) (map[string]any, bool, error)

func (Manifest) ToResourceState added in v0.51.0

func (m Manifest) ToResourceState(deployTarget string) sdk.ResourceState

ToResourceState converts the manifest into a sdk.ResourceState.

func (*Manifest) UnmarshalJSON

func (m *Manifest) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Manifest) YamlBytes added in v0.51.0

func (m *Manifest) YamlBytes() ([]byte, error)

type ResourceKey

type ResourceKey struct {
	// contains filtered or unexported fields
}

ResourceKey represents a unique key of a Kubernetes resource. We use GroupKind, namespace, and name to identify a resource.

func FindRemoveResources added in v0.51.0

func FindRemoveResources(manifests, namespacedLiveResources, clusterScopedLiveResources []Manifest) []ResourceKey

FindRemoveResources identifies resources that are present in the live state but not in the desired manifests. It doesn't return the resources that are not managed by Piped.

func (ResourceKey) Kind

func (k ResourceKey) Kind() string

func (ResourceKey) Name

func (k ResourceKey) Name() string

func (ResourceKey) Namespace

func (k ResourceKey) Namespace() string

func (ResourceKey) ReadableString

func (k ResourceKey) ReadableString() string

func (ResourceKey) String

func (k ResourceKey) String() string

type TemplatingMethod

type TemplatingMethod string
const (
	TemplatingMethodHelm      TemplatingMethod = "helm"
	TemplatingMethodKustomize TemplatingMethod = "kustomize"
	TemplatingMethodNone      TemplatingMethod = "none"
)

type ToolRegistry

type ToolRegistry interface {
	Kustomize(ctx context.Context, version string) (string, error)
	Helm(ctx context.Context, version string) (string, error)
}

type WorkloadPair added in v0.51.0

type WorkloadPair struct {
	Old Manifest
	New Manifest
}

WorkloadPair represents a pair of old and new manifests.

func FindSameManifests added in v0.51.0

func FindSameManifests(olds, news []Manifest) []WorkloadPair

FindSameManifests returns the pairs of old and new manifests that have the same key.

Jump to

Keyboard shortcuts

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