crossplane
142 строки · 4.1 Кб
1/*
2Copyright 2021 The Crossplane Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package v1beta1
18
19import (
20metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21
22"github.com/crossplane/crossplane/internal/dag"
23)
24
25var _ dag.Node = &Dependency{}
26var _ dag.Node = &LockPackage{}
27
28// A PackageType is a type of package.
29type PackageType string
30
31// Types of packages.
32const (
33ConfigurationPackageType PackageType = "Configuration"
34ProviderPackageType PackageType = "Provider"
35FunctionPackageType PackageType = "Function"
36)
37
38// LockPackage is a package that is in the lock.
39type LockPackage struct {
40// Name corresponds to the name of the package revision for this package.
41Name string `json:"name"`
42
43// Type is the type of package. Can be either Configuration or Provider.
44Type PackageType `json:"type"`
45
46// Source is the OCI image name without a tag or digest.
47Source string `json:"source"`
48
49// Version is the tag or digest of the OCI image.
50Version string `json:"version"`
51
52// Dependencies are the list of dependencies of this package. The order of
53// the dependencies will dictate the order in which they are resolved.
54Dependencies []Dependency `json:"dependencies"`
55}
56
57// ToNodes converts LockPackages to DAG nodes.
58func ToNodes(pkgs ...LockPackage) []dag.Node {
59nodes := make([]dag.Node, len(pkgs))
60for i, r := range pkgs {
61r := r // Pin range variable so we can take its address.
62nodes[i] = &r
63}
64return nodes
65}
66
67// Identifier returns the source of a LockPackage.
68func (l *LockPackage) Identifier() string {
69return l.Source
70}
71
72// Neighbors returns dependencies of a LockPackage.
73func (l *LockPackage) Neighbors() []dag.Node {
74nodes := make([]dag.Node, len(l.Dependencies))
75for i, r := range l.Dependencies {
76r := r // Pin range variable so we can take its address.
77nodes[i] = &r
78}
79return nodes
80}
81
82// AddNeighbors adds dependencies to a LockPackage. A LockPackage should always
83// have all dependencies declared before being added to the Lock, so we no-op
84// when adding a neighbor.
85func (l *LockPackage) AddNeighbors(_ ...dag.Node) error {
86return nil
87}
88
89// A Dependency is a dependency of a package in the lock.
90type Dependency struct {
91// Package is the OCI image name without a tag or digest.
92Package string `json:"package"`
93
94// Type is the type of package. Can be either Configuration or Provider.
95Type PackageType `json:"type"`
96
97// Constraints is a valid semver range, which will be used to select a valid
98// dependency version.
99Constraints string `json:"constraints"`
100}
101
102// Identifier returns a dependency's source.
103func (d *Dependency) Identifier() string {
104return d.Package
105}
106
107// Neighbors in is a no-op for dependencies because we are not yet aware of its
108// dependencies.
109func (d *Dependency) Neighbors() []dag.Node {
110return nil
111}
112
113// AddNeighbors is a no-op for dependencies. We should never be adding neighbors
114// to a dependency.
115func (d *Dependency) AddNeighbors(...dag.Node) error {
116return nil
117}
118
119// +kubebuilder:object:root=true
120// +genclient
121// +genclient:nonNamespaced
122
123// Lock is the CRD type that tracks package dependencies.
124// +kubebuilder:storageversion
125// +kubebuilder:subresource:status
126// +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp"
127// +kubebuilder:resource:scope=Cluster
128type Lock struct {
129metav1.TypeMeta `json:",inline"`
130metav1.ObjectMeta `json:"metadata,omitempty"`
131
132Packages []LockPackage `json:"packages,omitempty"`
133}
134
135// +kubebuilder:object:root=true
136
137// LockList contains a list of Lock.
138type LockList struct {
139metav1.TypeMeta `json:",inline"`
140metav1.ListMeta `json:"metadata,omitempty"`
141Items []Lock `json:"items"`
142}
143