Skip to content

Commit 0e6ae3b

Browse files
committed
Add Bucket type to API
1 parent 4f30ff1 commit 0e6ae3b

File tree

10 files changed

+969
-0
lines changed

10 files changed

+969
-0
lines changed

PROJECT

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ resources:
1010
- group: source
1111
kind: HelmChart
1212
version: v1alpha1
13+
- group: source
14+
kind: Bucket
15+
version: v1alpha1
1316
version: "2"

api/v1alpha1/bucket_types.go

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/*
2+
Copyright 2020 The Flux CD contributors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
"time"
21+
22+
corev1 "k8s.io/api/core/v1"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
)
25+
26+
const (
27+
BucketKind = "Bucket"
28+
BucketTimeout = time.Second * 20
29+
)
30+
31+
// BucketSpec defines the desired state of an S3 compatible bucket
32+
type BucketSpec struct {
33+
// The S3 compatible storage provider name, default ('generic').
34+
// +kubebuilder:validation:Enum=generic;aws
35+
// +optional
36+
Provider string `json:"provider,omitempty"`
37+
38+
// The bucket name.
39+
// +required
40+
BucketName string `json:"bucketName"`
41+
42+
// The bucket endpoint address.
43+
// +required
44+
Endpoint string `json:"endpoint"`
45+
46+
// Insecure allows connecting to a non-TLS S3 HTTP endpoint.
47+
// +optional
48+
Insecure bool `json:"insecure,omitempty"`
49+
50+
// The bucket region.
51+
// +optional
52+
Region string `json:"region,omitempty"`
53+
54+
// The secret name containing the bucket accesskey and secretkey.
55+
// +optional
56+
SecretRef *corev1.LocalObjectReference `json:"secretRef,omitempty"`
57+
58+
// The interval at which to check for bucket updates.
59+
// +required
60+
Interval metav1.Duration `json:"interval"`
61+
62+
// The timeout for download operations, default ('20s').
63+
// +optional
64+
Timeout *metav1.Duration `json:"timeout,omitempty"`
65+
66+
// Ignore overrides the set of excluded patterns in the .sourceignore
67+
// format (which is the same as .gitignore).
68+
// +optional
69+
Ignore *string `json:"ignore,omitempty"`
70+
}
71+
72+
// BucketStatus defines the observed state of a bucket
73+
type BucketStatus struct {
74+
// ObservedGeneration is the last observed generation.
75+
// +optional
76+
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
77+
78+
// Conditions holds the conditions for the Bucket.
79+
// +optional
80+
Conditions []SourceCondition `json:"conditions,omitempty"`
81+
82+
// URL is the download link for the artifact output of the last Bucket sync.
83+
// +optional
84+
URL string `json:"url,omitempty"`
85+
86+
// Artifact represents the output of the last successful Bucket sync.
87+
// +optional
88+
Artifact *Artifact `json:"artifact,omitempty"`
89+
}
90+
91+
const (
92+
// BucketOperationSucceedReason represents the fact that the bucket listing
93+
// and download operations succeeded.
94+
BucketOperationSucceedReason string = "BucketOperationSucceed"
95+
96+
// BucketOperationFailedReason represents the fact that the bucket listing
97+
// or download operations failed.
98+
BucketOperationFailedReason string = "BucketOperationFailed"
99+
)
100+
101+
// BucketProgressing resets the conditions of the Bucket
102+
// to SourceCondition of type Ready with status unknown and
103+
// progressing reason and message. It returns the modified Bucket.
104+
func BucketProgressing(bucket Bucket) Bucket {
105+
bucket.Status.ObservedGeneration = bucket.Generation
106+
bucket.Status.URL = ""
107+
bucket.Status.Conditions = []SourceCondition{}
108+
SetBucketCondition(&bucket, ReadyCondition, corev1.ConditionUnknown, ProgressingReason, "reconciliation in progress")
109+
return bucket
110+
}
111+
112+
// SetBucketCondition sets the given condition with the given status, reason and message on the Bucket.
113+
func SetBucketCondition(bucket *Bucket, condition string, status corev1.ConditionStatus, reason, message string) {
114+
bucket.Status.Conditions = filterOutSourceCondition(bucket.Status.Conditions, condition)
115+
bucket.Status.Conditions = append(bucket.Status.Conditions, SourceCondition{
116+
Type: condition,
117+
Status: status,
118+
LastTransitionTime: metav1.Now(),
119+
Reason: reason,
120+
Message: message,
121+
})
122+
}
123+
124+
// BucketReady sets the given artifact and url on the Bucket
125+
// and sets the ReadyCondition to True, with the given reason and
126+
// message. It returns the modified Bucket.
127+
func BucketReady(repository Bucket, artifact Artifact, url, reason, message string) Bucket {
128+
repository.Status.Artifact = &artifact
129+
repository.Status.URL = url
130+
SetBucketCondition(&repository, ReadyCondition, corev1.ConditionTrue, reason, message)
131+
return repository
132+
}
133+
134+
// BucketNotReady sets the ReadyCondition on the given Bucket
135+
// to False, with the given reason and message. It returns the modified Bucket.
136+
func BucketNotReady(repository Bucket, reason, message string) Bucket {
137+
SetBucketCondition(&repository, ReadyCondition, corev1.ConditionFalse, reason, message)
138+
return repository
139+
}
140+
141+
// BucketReadyMessage returns the message of the SourceCondition
142+
// of type Ready with status true if present, or an empty string.
143+
func BucketReadyMessage(repository Bucket) string {
144+
for _, condition := range repository.Status.Conditions {
145+
if condition.Type == ReadyCondition && condition.Status == corev1.ConditionTrue {
146+
return condition.Message
147+
}
148+
}
149+
return ""
150+
}
151+
152+
// GetTimeout returns the configured timeout or the default.
153+
func (in *Bucket) GetTimeout() time.Duration {
154+
if in.Spec.Timeout != nil {
155+
return in.Spec.Timeout.Duration
156+
}
157+
return BucketTimeout
158+
}
159+
160+
// GetArtifact returns the latest artifact from the source
161+
// if present in the status sub-resource.
162+
func (in *Bucket) GetArtifact() *Artifact {
163+
return in.Status.Artifact
164+
}
165+
166+
// GetInterval returns the interval at which the source is updated.
167+
func (in *Bucket) GetInterval() metav1.Duration {
168+
return in.Spec.Interval
169+
}
170+
171+
// +genclient
172+
// +genclient:Namespaced
173+
// +kubebuilder:object:root=true
174+
// +kubebuilder:subresource:status
175+
// +kubebuilder:printcolumn:name="URL",type=string,JSONPath=`.spec.url`
176+
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].status",description=""
177+
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].message",description=""
178+
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description=""
179+
180+
// Bucket is the Schema for the buckets API
181+
type Bucket struct {
182+
metav1.TypeMeta `json:",inline"`
183+
metav1.ObjectMeta `json:"metadata,omitempty"`
184+
185+
Spec BucketSpec `json:"spec,omitempty"`
186+
Status BucketStatus `json:"status,omitempty"`
187+
}
188+
189+
// +kubebuilder:object:root=true
190+
191+
// BucketList contains a list of Bucket
192+
type BucketList struct {
193+
metav1.TypeMeta `json:",inline"`
194+
metav1.ListMeta `json:"metadata,omitempty"`
195+
Items []Bucket `json:"items"`
196+
}
197+
198+
func init() {
199+
SchemeBuilder.Register(&Bucket{}, &BucketList{})
200+
}

api/v1alpha1/zz_generated.deepcopy.go

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

0 commit comments

Comments
 (0)