Skip to content

Commit 5f2a6eb

Browse files
authored
Merge pull request #5583 from dgunzy/add-export-source-external
[RFC-0012] Add command `flux export source external`
2 parents 60e4d99 + cdc37c3 commit 5f2a6eb

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

cmd/flux/export_source_external.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright 2025 The Flux authors
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 main
18+
19+
import (
20+
"github.com/spf13/cobra"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
"k8s.io/apimachinery/pkg/types"
23+
24+
sourcev1 "github.com/fluxcd/source-controller/api/v1"
25+
)
26+
27+
var exportSourceExternalCmd = &cobra.Command{
28+
Use: "external [name]",
29+
Short: "Export ExternalArtifact sources in YAML format",
30+
Long: "The export source external command exports one or all ExternalArtifact sources in YAML format.",
31+
Example: ` # Export all ExternalArtifact sources
32+
flux export source external --all > sources.yaml
33+
34+
# Export a specific ExternalArtifact
35+
flux export source external my-artifact > source.yaml`,
36+
ValidArgsFunction: resourceNamesCompletionFunc(sourcev1.GroupVersion.WithKind(sourcev1.ExternalArtifactKind)),
37+
RunE: exportWithSecretCommand{
38+
list: externalArtifactListAdapter{&sourcev1.ExternalArtifactList{}},
39+
object: externalArtifactAdapter{&sourcev1.ExternalArtifact{}},
40+
}.run,
41+
}
42+
43+
func init() {
44+
exportSourceCmd.AddCommand(exportSourceExternalCmd)
45+
}
46+
47+
func exportExternalArtifact(source *sourcev1.ExternalArtifact) any {
48+
gvk := sourcev1.GroupVersion.WithKind(sourcev1.ExternalArtifactKind)
49+
export := sourcev1.ExternalArtifact{
50+
TypeMeta: metav1.TypeMeta{
51+
Kind: gvk.Kind,
52+
APIVersion: gvk.GroupVersion().String(),
53+
},
54+
ObjectMeta: metav1.ObjectMeta{
55+
Name: source.Name,
56+
Namespace: source.Namespace,
57+
Labels: source.Labels,
58+
Annotations: source.Annotations,
59+
},
60+
Spec: source.Spec,
61+
}
62+
return export
63+
}
64+
65+
func getExternalArtifactSecret(source *sourcev1.ExternalArtifact) *types.NamespacedName {
66+
// ExternalArtifact does not have a secretRef in its spec, this satisfies the interface
67+
return nil
68+
}
69+
70+
func (ex externalArtifactAdapter) secret() *types.NamespacedName {
71+
return getExternalArtifactSecret(ex.ExternalArtifact)
72+
}
73+
74+
func (ex externalArtifactListAdapter) secretItem(i int) *types.NamespacedName {
75+
return getExternalArtifactSecret(&ex.ExternalArtifactList.Items[i])
76+
}
77+
78+
func (ex externalArtifactAdapter) export() any {
79+
return exportExternalArtifact(ex.ExternalArtifact)
80+
}
81+
82+
func (ex externalArtifactListAdapter) exportItem(i int) any {
83+
return exportExternalArtifact(&ex.ExternalArtifactList.Items[i])
84+
}

cmd/flux/export_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ func TestExport(t *testing.T) {
110110
"testdata/export/bucket.yaml",
111111
tmpl,
112112
},
113+
{
114+
"source external",
115+
"export source external flux-system",
116+
"testdata/export/external-artifact.yaml",
117+
tmpl,
118+
},
113119
}
114120

115121
for _, tt := range cases {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
apiVersion: source.toolkit.fluxcd.io/v1
3+
kind: ExternalArtifact
4+
metadata:
5+
name: flux-system
6+
namespace: {{ .fluxns }}
7+
spec:
8+
sourceRef:
9+
apiVersion: source.example.com/v1alpha1
10+
kind: GitHubRelease
11+
name: flux-system
12+
namespace: {{ .fluxns }}

cmd/flux/testdata/export/objects.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,15 @@ spec:
165165
endpoint: s3.amazonaws.com
166166
region: us-east-1
167167
timeout: 30s
168+
---
169+
apiVersion: source.toolkit.fluxcd.io/v1
170+
kind: ExternalArtifact
171+
metadata:
172+
name: flux-system
173+
namespace: {{ .fluxns }}
174+
spec:
175+
sourceRef:
176+
apiVersion: source.example.com/v1alpha1
177+
kind: GitHubRelease
178+
name: flux-system
179+
namespace: {{ .fluxns }}

0 commit comments

Comments
 (0)