provider

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2025 License: Apache-2.0 Imports: 32 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
	LabelIgnoreDriftDirection = "pipecd.dev/ignore-drift-detection" // Whether the drift detection should ignore this resource.

	// 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"
	IgnoreDriftDetectionTrue = "true"
)
View Source
const (
	// Service
	KindService = "Service"

	// Workload
	KindDeployment = "Deployment"
	KindReplicaSet = "ReplicaSet"
	KindDaemonSet  = "DaemonSet"
	KindPod        = "Pod"

	// ConfigMap and Secret
	KindSecret    = "Secret"
	KindConfigMap = "ConfigMap"

	DefaultNamespace = "default"
)

Variables

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

Functions

func Diff

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

func FindConfigsAndSecrets

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

FindConfigsAndSecrets returns the manifests that are ConfigMap or Secret.

func FindContainerImages

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

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

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

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

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

HashManifests computes the hash of a list of manifests.

Types

type Applier

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

func NewApplier

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

func (*Applier) ApplyManifest

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

ApplyManifest does applying the given manifest.

func (*Applier) CreateManifest

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

CreateManifest uses kubectl to create the given manifests.

func (*Applier) Delete

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

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

ForceReplaceManifest uses kubectl to forcefully replace the given manifests.

func (*Applier) ReplaceManifest

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

ReplaceManifest uses kubectl to replace the given manifests.

type DiffListChange

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

type DiffListResult

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

func DiffList

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

func (*DiffListResult) NoChanges

func (r *DiffListResult) NoChanges() bool

func (*DiffListResult) Render

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

func (*DiffListResult) TotalOutOfSync

func (r *DiffListResult) TotalOutOfSync() int

type DiffRenderOptions

type DiffRenderOptions struct {
	MaskSecret    bool
	MaskConfigMap bool
	// Maximum number of changed manifests should be shown.
	// Zero means rendering all.
	MaxChangedManifests int
	// If true, use "diff" command to render.
	UseDiffCommand bool
}

type Helm

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

func NewHelm

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

func (*Helm) AddRepository

func (h *Helm) AddRepository(ctx context.Context, repo config.HelmChartRepository) error

Add installs all specified Helm Chart repositories. https://helm.sh/docs/topics/chart_repository/ helm repo add fantastic-charts https://fantastic-charts.storage.googleapis.com helm repo add fantastic-charts https://fantastic-charts.storage.googleapis.com --username my-username --password my-password

func (*Helm) LoginToOCIRegistry

func (h *Helm) LoginToOCIRegistry(ctx context.Context, address, username, password string) error

func (*Helm) TemplateLocalChart

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

func (*Helm) TemplateRemoteChart

func (h *Helm) TemplateRemoteChart(ctx context.Context, appName, appDir, namespace string, chart helmRemoteChart, opts *config.InputHelmOptions) (string, error)

func (*Helm) UpdateRepositories

func (h *Helm) UpdateRepositories(ctx context.Context) error

type Kubectl

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

Kubectl is a wrapper for kubectl command.

func NewKubectl

func NewKubectl(path string) *Kubectl

NewKubectl creates a new Kubectl instance.

func (*Kubectl) Apply

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

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

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

CreateNamespace runs kubectl create namespace with the given namespace.

func (*Kubectl) Delete

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

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

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

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

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

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

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

	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 DeepCopyManifests

func DeepCopyManifests(manifests []Manifest) []Manifest

DeepCopyManifests returns a deep copy of the given manifests.

func FromStructuredObject

func FromStructuredObject(o any) (Manifest, error)

FromStructuredObject creates a new Manifest from a structured Kubernetes object.

func FromUnstructured

func FromUnstructured(u *unstructured.Unstructured) Manifest

FromUnstructured creates a new Manifest from a Kubernetes unstructured object.

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

func (m Manifest) APIVersion() string

func (Manifest) AddAnnotations

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

func (Manifest) AddLabels

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

func (Manifest) AddStringMapValues

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

func (m Manifest) ApplicationID() string

ApplicationID returns the application ID of the resource.

func (Manifest) ConvertToStructuredObject

func (m Manifest) ConvertToStructuredObject(o any) 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 uses the runtime.DefaultUnstructuredConverter to convert the manifest into the provided object.

func (Manifest) DeepCopy

func (m Manifest) DeepCopy() Manifest

DeepCopy returns a deep copy of the manifest.

func (Manifest) DeepCopyWithName

func (m Manifest) DeepCopyWithName(name string) Manifest

DeepCopyWithName returns a deep copy of the manifest with the given name.

func (Manifest) GetAnnotations

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

func (Manifest) GroupVersionKind

func (m Manifest) GroupVersionKind() schema.GroupVersionKind

func (Manifest) IsConfigMap

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

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

func (m Manifest) IsManagedByPiped() bool

IsManagedByPiped returns true if the manifest is managed by Piped.

func (Manifest) IsSecret

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

func (m Manifest) IsService() bool

IsService returns true if the manifest is a Service. It checks the API group and the kind of the manifest.

func (Manifest) IsWorkload

func (m Manifest) IsWorkload() bool

IsWorkload returns true if the manifest is a Deployment, StatefulSet, or DaemonSet. It checks the API group and the kind of the manifest.

func (Manifest) Key

func (m Manifest) Key() ResourceKey

func (Manifest) Kind

func (m Manifest) Kind() string

func (*Manifest) MarshalJSON

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

func (m Manifest) Name() string

func (Manifest) NestedMap

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

func (Manifest) NestedString

func (m Manifest) NestedString(fields ...string) (string, bool, error)

func (Manifest) OwnerReferences

func (m Manifest) OwnerReferences() []types.UID

OwnerReferences returns the owner references of the resource.

func (Manifest) ToResourceState

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

ToResourceState converts the manifest into a sdk.ResourceState.

func (Manifest) UID

func (m Manifest) UID() types.UID

UID returns the UID of the resource. This will be empty when this manifest is loaded form the manifest file. This will be non-empty when this manifest is loaded from the live state.

func (*Manifest) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Manifest) YamlBytes

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

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

type WorkloadPair struct {
	Old Manifest
	New Manifest
}

WorkloadPair represents a pair of old and new manifests.

func FindSameManifests

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