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

feat: add Artifact object #1703

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ resources:
- group: source
kind: Bucket
version: v1
- group: source
kind: Artifact
version: v1alpha1
version: "2"
87 changes: 87 additions & 0 deletions api/v1alpha1/artifact_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Copyright 2024 The Flux authors

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 v1alpha1

import (
"time"

v1 "github.com/fluxcd/source-controller/api/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
// BucketKind is the string representation of a Bucket.
ArtifactKind = "Artifact"
)

// ArtifactSpec defines the desired state of Artifact
type ArtifactSpec struct {

// +kubebuilder:validation:Required
// +kubebuilder:validation:MinLength=1
CurrentVersion string `json:"currentVersion,omitempty"`

// +kubebuilder:validation:Required
Versions map[string]*v1.Artifact `json:"versions,omitempty"`
}
Comment on lines +31 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// ArtifactSpec defines the desired state of Artifact
type ArtifactSpec struct {
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinLength=1
CurrentVersion string `json:"currentVersion,omitempty"`
// +kubebuilder:validation:Required
Versions map[string]*v1.Artifact `json:"versions,omitempty"`
}
// ArtifactSpec defines the desired state of Artifact
type ArtifactSpec v1.Artifact

Copy link
Author

@guilhem guilhem Dec 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's really part of the discussion.
Having only the latest artifact in spec makes it simpler, but also points some problems:

  • status is not made to be backup / stateful. So customers can't rely on it.
  • having status quite implies a controller to manage changes from spec to status.

With everything in spec, it makes things trusty and "backupable":

  • Any producer is responsible for content with any dedicated controller involved.
  • Any consumer can decide to stay on an older version (if user asks “on hold”, for example)

I don't see that many usages of "Status" for an object like an Artifact (but I may be wrong).
But events produced by “consumers” can be a great thing.

Copy link
Member

@stefanprodan stefanprodan Dec 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

status is not made to be backup / stateful. So customers can't rely on it.

For sure, no user should relay on anything from this custom resource, status or not is irelevant. This custom resource is a side-efect of some 3rd-party source-controller that should fully manage the artifact. This type of object is not "desired state" it shouldn't be included in the backup. You should backup what ever custom resource your controller uses for "desired state".

source-controller-x would reconcile GitRepositoryX objects and would create/update/delete ArtifactX objects. If you want to pin your source to a Git commit, the user will do this in the GitRepositoryX. Users will never interact with ArtifactX as the artifact is a result of the reconciliation, it's not the "desired state", the artifact is the "actual state".

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok for me, just wanted to know if the desired state should be managed by source controller and just an information storage :)

And yes, I'm developing a source-controller for Omaha / Nebraska, so I will need a way to interact with consumers controllers :)

Copy link
Member

@stefanprodan stefanprodan Dec 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flux source-controller plays no role here, this ArtifactX type is to enable other source controllers to feed data to Flux kustomize-controller. We discussed this in the dev meetings, the artifact for the native Flux sources will remain unchanged, it's part of the .status. For 3rd-party source controllers, they will have to generate the artifact in a standalone object that can be referenced in Flux Kustomizations.


// ArtifactStatus defines the observed state of Artifact
type ArtifactStatus struct {
}
Comment on lines +43 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type ArtifactStatus struct {
}
type ArtifactStatus struct {
// +kubebuilder:validation:Required
History []v1.Artifact `json:"history"`
}

This is how we track versions in Flux APIs, the first entry is the current version, all this logic is already in helm-controller.


// +genclient
// +kubebuilder:storageversion
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// Artifact is the Schema for the artifacts API
type Artifact struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ArtifactSpec `json:"spec,omitempty"`
Status ArtifactStatus `json:"status,omitempty"`
}

// GetArtifact returns the latest Artifact from the Artifact if present in
// the status sub-resource.
func (in *Artifact) GetArtifact() *v1.Artifact {
if in.Spec.CurrentVersion == "" {
return nil
}
if in.Spec.Versions == nil {
return nil
}
return in.Spec.Versions[in.Spec.CurrentVersion]
}

func (in *Artifact) GetRequeueAfter() time.Duration {
return time.Minute
}

// +kubebuilder:object:root=true

// ArtifactList contains a list of Artifact
type ArtifactList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Artifact `json:"items"`
}

func init() {
SchemeBuilder.Register(&Artifact{}, &ArtifactList{})
}
36 changes: 36 additions & 0 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2024 The Flux authors

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 v1alpha1 contains API Schema definitions for the source v1alpha1 API group
// +kubebuilder:object:generate=true
// +groupName=source.toolkit.fluxcd.io
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "source.toolkit.fluxcd.io", Version: "v1alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
131 changes: 131 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

1 change: 1 addition & 0 deletions config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ resources:
- bases/source.toolkit.fluxcd.io_helmcharts.yaml
- bases/source.toolkit.fluxcd.io_buckets.yaml
- bases/source.toolkit.fluxcd.io_ocirepositories.yaml
- bases/source.toolkit.fluxcd.io_artifacts.yaml
# +kubebuilder:scaffold:crdkustomizeresource
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"github.com/fluxcd/pkg/runtime/probes"

"github.com/fluxcd/source-controller/api/v1"
"github.com/fluxcd/source-controller/api/v1alpha1"
"github.com/fluxcd/source-controller/api/v1beta2"

// +kubebuilder:scaffold:imports
Expand Down Expand Up @@ -85,6 +86,7 @@ func init() {

utilruntime.Must(v1beta2.AddToScheme(scheme))
utilruntime.Must(v1.AddToScheme(scheme))
utilruntime.Must(v1alpha1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}

Expand Down