Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

packagevariantset controller: support arbitrary object selectors #3764

Merged
merged 4 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,75 @@ spec:
status:
description: PackageVariantSetStatus defines the observed state of PackageVariantSet
properties:
validationErrors:
description: 'TODO: Move this to conditions.'
conditions:
description: Conditions describes the reconciliation state of the
object.
items:
type: string
description: "Condition contains details for one aspect of the current
state of this API Resource. --- This struct is intended for direct
use as an array at the field path .status.conditions. For example,
\n type FooStatus struct{ // Represents the observations of a
foo's current state. // Known .status.conditions.type are: \"Available\",
\"Progressing\", and \"Degraded\" // +patchMergeKey=type // +patchStrategy=merge
// +listType=map // +listMapKey=type Conditions []metav1.Condition
`json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\"
protobuf:\"bytes,1,rep,name=conditions\"` \n // other fields }"
properties:
lastTransitionTime:
description: lastTransitionTime is the last time the condition
transitioned from one status to another. This should be when
the underlying condition changed. If that is not known, then
using the time when the API field changed is acceptable.
format: date-time
type: string
message:
description: message is a human readable message indicating
details about the transition. This may be an empty string.
maxLength: 32768
type: string
observedGeneration:
description: observedGeneration represents the .metadata.generation
that the condition was set based upon. For instance, if .metadata.generation
is currently 12, but the .status.conditions[x].observedGeneration
is 9, the condition is out of date with respect to the current
state of the instance.
format: int64
minimum: 0
type: integer
reason:
description: reason contains a programmatic identifier indicating
the reason for the condition's last transition. Producers
of specific condition types may define expected values and
meanings for this field, and whether the values are considered
a guaranteed API. The value should be a CamelCase string.
This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: status of the condition, one of True, False, Unknown.
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: type of condition in CamelCase or in foo.example.com/CamelCase.
--- Many .condition.type values are consistent across resources
like Available, but because arbitrary conditions can be useful
(see .node.status.conditions), the ability to deconflict is
important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt)
maxLength: 316
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])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
type: object
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package v1alpha1

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

func (s *Selector) ToKptfileSelector() kptfilev1.Selector {
var labels map[string]string
if s.Labels != nil {
labels = s.Labels.MatchLabels
}
return kptfilev1.Selector{
APIVersion: s.APIVersion,
Kind: s.Kind,
Name: s.Name,
Namespace: s.Namespace,
Labels: labels,
Annotations: s.Annotations,
}
}

type PackageName struct {
Name *ValueOrFromField `json:"baseName,omitempty"`

Expand All @@ -116,8 +132,8 @@ type ValueOrFromField struct {

// PackageVariantSetStatus defines the observed state of PackageVariantSet
type PackageVariantSetStatus struct {
// TODO: Move this to conditions.
ValidationErrors []string `json:"validationErrors,omitempty"`
// Conditions describes the reconciliation state of the object.
Conditions []metav1.Condition `json:"conditions,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions porch/controllers/packagevariantsets/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ rules:
- get
- patch
- update
- apiGroups: ["*"]
resources: ["*"]
verbs: ["list"]
11 changes: 10 additions & 1 deletion porch/controllers/packagevariantsets/config/samples/pvs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@ spec:
packageName:
baseName:
value: beta

- objects:
selectors:
- apiVersion: v1
kind: Pod
name: my-pod
repoName:
value: blueprints
packageName:
baseName:
value: gamma
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package packagevariantset

import (
"context"
"fmt"

pkgvarapi "github.com/GoogleContainerTools/kpt/porch/controllers/packagevariants/api/v1alpha1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
)

type fakeClient struct {
objects []client.Object
client.Client
}

var _ client.Client = &fakeClient{}

func (f *fakeClient) Create(_ context.Context, obj client.Object, _ ...client.CreateOption) error {
f.objects = append(f.objects, obj)
return nil
}

func (f *fakeClient) Delete(_ context.Context, obj client.Object, _ ...client.DeleteOption) error {
var newObjects []client.Object
for _, old := range f.objects {
if obj.GetName() != old.GetName() {
newObjects = append(newObjects, old)
}
}
f.objects = newObjects
return nil
}

func (f *fakeClient) List(_ context.Context, obj client.ObjectList, _ ...client.ListOption) error {
podList := `apiVersion: v1
kind: PodList
metadata:
name: my-pod-list
items:
- apiVersion: v1
kind: Pod
metadata:
name: my-pod-1
labels:
foo: bar
abc: def
- apiVersion: v1
kind: Pod
metadata:
name: my-pod-2
labels:
abc: def
efg: hij`

pvList := `apiVersion: config.porch.kpt.dev
kind: PackageVariantList
metadata:
name: my-pv-list
items:
- apiVersion: config.porch.kpt.dev
kind: PackageVariant
metadata:
name: my-pv-1
spec:
upstream:
repo: up
package: up
revision: up
downstream:
repo: dn-1
package: dn-1
- apiVersion: config.porch.kpt.dev
kind: PackageVariant
metadata:
name: my-pv-2
spec:
upstream:
repo: up
package: up
revision: up
downstream:
repo: dn-2
package: dn-2`

var err error
switch v := obj.(type) {
case *unstructured.UnstructuredList:
err = yaml.Unmarshal([]byte(podList), v)
for _, o := range v.Items {
f.objects = append(f.objects, o.DeepCopy())
}
case *pkgvarapi.PackageVariantList:
err = yaml.Unmarshal([]byte(pvList), v)
for _, o := range v.Items {
f.objects = append(f.objects, o.DeepCopy())
}
default:
return fmt.Errorf("unsupported type")
}
return err
}