Skip to content

Commit 5cd9a99

Browse files
authored
packagevariantset controller: support arbitrary object selectors (#3764)
1 parent 0b636c4 commit 5cd9a99

File tree

8 files changed

+438
-45
lines changed

8 files changed

+438
-45
lines changed

porch/controllers/config/crd/bases/config.porch.kpt.dev_packagevariantsets.yaml

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,75 @@ spec:
245245
status:
246246
description: PackageVariantSetStatus defines the observed state of PackageVariantSet
247247
properties:
248-
validationErrors:
249-
description: 'TODO: Move this to conditions.'
248+
conditions:
249+
description: Conditions describes the reconciliation state of the
250+
object.
250251
items:
251-
type: string
252+
description: "Condition contains details for one aspect of the current
253+
state of this API Resource. --- This struct is intended for direct
254+
use as an array at the field path .status.conditions. For example,
255+
\n type FooStatus struct{ // Represents the observations of a
256+
foo's current state. // Known .status.conditions.type are: \"Available\",
257+
\"Progressing\", and \"Degraded\" // +patchMergeKey=type // +patchStrategy=merge
258+
// +listType=map // +listMapKey=type Conditions []metav1.Condition
259+
`json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\"
260+
protobuf:\"bytes,1,rep,name=conditions\"` \n // other fields }"
261+
properties:
262+
lastTransitionTime:
263+
description: lastTransitionTime is the last time the condition
264+
transitioned from one status to another. This should be when
265+
the underlying condition changed. If that is not known, then
266+
using the time when the API field changed is acceptable.
267+
format: date-time
268+
type: string
269+
message:
270+
description: message is a human readable message indicating
271+
details about the transition. This may be an empty string.
272+
maxLength: 32768
273+
type: string
274+
observedGeneration:
275+
description: observedGeneration represents the .metadata.generation
276+
that the condition was set based upon. For instance, if .metadata.generation
277+
is currently 12, but the .status.conditions[x].observedGeneration
278+
is 9, the condition is out of date with respect to the current
279+
state of the instance.
280+
format: int64
281+
minimum: 0
282+
type: integer
283+
reason:
284+
description: reason contains a programmatic identifier indicating
285+
the reason for the condition's last transition. Producers
286+
of specific condition types may define expected values and
287+
meanings for this field, and whether the values are considered
288+
a guaranteed API. The value should be a CamelCase string.
289+
This field may not be empty.
290+
maxLength: 1024
291+
minLength: 1
292+
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
293+
type: string
294+
status:
295+
description: status of the condition, one of True, False, Unknown.
296+
enum:
297+
- "True"
298+
- "False"
299+
- Unknown
300+
type: string
301+
type:
302+
description: type of condition in CamelCase or in foo.example.com/CamelCase.
303+
--- Many .condition.type values are consistent across resources
304+
like Available, but because arbitrary conditions can be useful
305+
(see .node.status.conditions), the ability to deconflict is
306+
important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt)
307+
maxLength: 316
308+
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
309+
type: string
310+
required:
311+
- lastTransitionTime
312+
- message
313+
- reason
314+
- status
315+
- type
316+
type: object
252317
type: array
253318
type: object
254319
type: object

porch/controllers/packagevariantsets/api/v1alpha1/packagevariantset_types.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package v1alpha1
1616

1717
import (
18+
kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
1819
pkgvarapi "github.com/GoogleContainerTools/kpt/porch/controllers/packagevariants/api/v1alpha1"
1920
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2021
)
@@ -101,6 +102,21 @@ type Selector struct {
101102
Annotations map[string]string `yaml:"annotations,omitempty" json:"annotations,omitempty"`
102103
}
103104

105+
func (s *Selector) ToKptfileSelector() kptfilev1.Selector {
106+
var labels map[string]string
107+
if s.Labels != nil {
108+
labels = s.Labels.MatchLabels
109+
}
110+
return kptfilev1.Selector{
111+
APIVersion: s.APIVersion,
112+
Kind: s.Kind,
113+
Name: s.Name,
114+
Namespace: s.Namespace,
115+
Labels: labels,
116+
Annotations: s.Annotations,
117+
}
118+
}
119+
104120
type PackageName struct {
105121
Name *ValueOrFromField `json:"baseName,omitempty"`
106122

@@ -116,8 +132,8 @@ type ValueOrFromField struct {
116132

117133
// PackageVariantSetStatus defines the observed state of PackageVariantSet
118134
type PackageVariantSetStatus struct {
119-
// TODO: Move this to conditions.
120-
ValidationErrors []string `json:"validationErrors,omitempty"`
135+
// Conditions describes the reconciliation state of the object.
136+
Conditions []metav1.Condition `json:"conditions,omitempty"`
121137
}
122138

123139
//+kubebuilder:object:root=true

porch/controllers/packagevariantsets/api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

porch/controllers/packagevariantsets/config/rbac/role.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,6 @@ rules:
4646
- get
4747
- patch
4848
- update
49+
- apiGroups: ["*"]
50+
resources: ["*"]
51+
verbs: ["list"]

porch/controllers/packagevariantsets/config/samples/pvs.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,13 @@ spec:
3333
packageName:
3434
baseName:
3535
value: beta
36-
36+
- objects:
37+
selectors:
38+
- apiVersion: v1
39+
kind: Pod
40+
name: my-pod
41+
repoName:
42+
value: blueprints
43+
packageName:
44+
baseName:
45+
value: gamma
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package packagevariantset
16+
17+
import (
18+
"context"
19+
"fmt"
20+
21+
pkgvarapi "github.com/GoogleContainerTools/kpt/porch/controllers/packagevariants/api/v1alpha1"
22+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
23+
"sigs.k8s.io/controller-runtime/pkg/client"
24+
"sigs.k8s.io/yaml"
25+
)
26+
27+
type fakeClient struct {
28+
objects []client.Object
29+
client.Client
30+
}
31+
32+
var _ client.Client = &fakeClient{}
33+
34+
func (f *fakeClient) Create(_ context.Context, obj client.Object, _ ...client.CreateOption) error {
35+
f.objects = append(f.objects, obj)
36+
return nil
37+
}
38+
39+
func (f *fakeClient) Delete(_ context.Context, obj client.Object, _ ...client.DeleteOption) error {
40+
var newObjects []client.Object
41+
for _, old := range f.objects {
42+
if obj.GetName() != old.GetName() {
43+
newObjects = append(newObjects, old)
44+
}
45+
}
46+
f.objects = newObjects
47+
return nil
48+
}
49+
50+
func (f *fakeClient) List(_ context.Context, obj client.ObjectList, _ ...client.ListOption) error {
51+
podList := `apiVersion: v1
52+
kind: PodList
53+
metadata:
54+
name: my-pod-list
55+
items:
56+
- apiVersion: v1
57+
kind: Pod
58+
metadata:
59+
name: my-pod-1
60+
labels:
61+
foo: bar
62+
abc: def
63+
- apiVersion: v1
64+
kind: Pod
65+
metadata:
66+
name: my-pod-2
67+
labels:
68+
abc: def
69+
efg: hij`
70+
71+
pvList := `apiVersion: config.porch.kpt.dev
72+
kind: PackageVariantList
73+
metadata:
74+
name: my-pv-list
75+
items:
76+
- apiVersion: config.porch.kpt.dev
77+
kind: PackageVariant
78+
metadata:
79+
name: my-pv-1
80+
spec:
81+
upstream:
82+
repo: up
83+
package: up
84+
revision: up
85+
downstream:
86+
repo: dn-1
87+
package: dn-1
88+
- apiVersion: config.porch.kpt.dev
89+
kind: PackageVariant
90+
metadata:
91+
name: my-pv-2
92+
spec:
93+
upstream:
94+
repo: up
95+
package: up
96+
revision: up
97+
downstream:
98+
repo: dn-2
99+
package: dn-2`
100+
101+
var err error
102+
switch v := obj.(type) {
103+
case *unstructured.UnstructuredList:
104+
err = yaml.Unmarshal([]byte(podList), v)
105+
for _, o := range v.Items {
106+
f.objects = append(f.objects, o.DeepCopy())
107+
}
108+
case *pkgvarapi.PackageVariantList:
109+
err = yaml.Unmarshal([]byte(pvList), v)
110+
for _, o := range v.Items {
111+
f.objects = append(f.objects, o.DeepCopy())
112+
}
113+
default:
114+
return fmt.Errorf("unsupported type")
115+
}
116+
return err
117+
}

0 commit comments

Comments
 (0)