Skip to content

Commit 9f18062

Browse files
authored
Merge pull request #5555 from dgunzy/add-get-source-external-artifact
[RFC-0012] Add command `flux get source external`
2 parents 7b0021c + 1055f28 commit 9f18062

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

cmd/flux/get_source_all.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ var getSourceAllCmd = &cobra.Command{
5959
apiType: helmChartType,
6060
list: &helmChartListAdapter{&sourcev1.HelmChartList{}},
6161
},
62+
{
63+
apiType: externalArtifactType,
64+
list: &externalArtifactListAdapter{&sourcev1.ExternalArtifactList{}},
65+
},
6266
}
6367

6468
for _, c := range allSourceCmd {

cmd/flux/get_source_external.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
"k8s.io/apimachinery/pkg/runtime"
24+
25+
sourcev1 "github.com/fluxcd/source-controller/api/v1"
26+
27+
"github.com/fluxcd/flux2/v2/internal/utils"
28+
)
29+
30+
var getSourceExternalCmd = &cobra.Command{
31+
Use: "external",
32+
Short: "Get ExternalArtifact source statuses",
33+
Long: `The get sources external command prints the status of the ExternalArtifact sources.`,
34+
Example: ` # List all ExternalArtifacts and their status
35+
flux get sources external
36+
37+
# List ExternalArtifacts from all namespaces
38+
flux get sources external --all-namespaces`,
39+
ValidArgsFunction: resourceNamesCompletionFunc(sourcev1.GroupVersion.WithKind(sourcev1.ExternalArtifactKind)),
40+
RunE: func(cmd *cobra.Command, args []string) error {
41+
get := getCommand{
42+
apiType: externalArtifactType,
43+
list: &externalArtifactListAdapter{&sourcev1.ExternalArtifactList{}},
44+
funcMap: make(typeMap),
45+
}
46+
47+
err := get.funcMap.registerCommand(get.apiType.kind, func(obj runtime.Object) (summarisable, error) {
48+
o, ok := obj.(*sourcev1.ExternalArtifact)
49+
if !ok {
50+
return nil, fmt.Errorf("impossible to cast type %#v to ExternalArtifact", obj)
51+
}
52+
53+
sink := &externalArtifactListAdapter{&sourcev1.ExternalArtifactList{
54+
Items: []sourcev1.ExternalArtifact{
55+
*o,
56+
}}}
57+
return sink, nil
58+
})
59+
60+
if err != nil {
61+
return err
62+
}
63+
64+
if err := get.run(cmd, args); err != nil {
65+
return err
66+
}
67+
68+
return nil
69+
},
70+
}
71+
72+
func init() {
73+
getSourceCmd.AddCommand(getSourceExternalCmd)
74+
}
75+
76+
func (a *externalArtifactListAdapter) summariseItem(i int, includeNamespace bool, includeKind bool) []string {
77+
item := a.Items[i]
78+
var revision string
79+
if item.Status.Artifact != nil {
80+
revision = item.Status.Artifact.Revision
81+
}
82+
status, msg := statusAndMessage(item.Status.Conditions)
83+
revision = utils.TruncateHex(revision)
84+
msg = utils.TruncateHex(msg)
85+
86+
var source string
87+
if item.Spec.SourceRef != nil {
88+
source = fmt.Sprintf("%s/%s/%s",
89+
item.Spec.SourceRef.Kind,
90+
item.Spec.SourceRef.Namespace,
91+
item.Spec.SourceRef.Name)
92+
}
93+
return append(nameColumns(&item, includeNamespace, includeKind),
94+
revision, source, status, msg)
95+
}
96+
97+
func (a externalArtifactListAdapter) headers(includeNamespace bool) []string {
98+
headers := []string{"Name", "Revision", "Source", "Ready", "Message"}
99+
if includeNamespace {
100+
headers = append([]string{"Namespace"}, headers...)
101+
}
102+
return headers
103+
}
104+
105+
func (a externalArtifactListAdapter) statusSelectorMatches(i int, conditionType, conditionStatus string) bool {
106+
item := a.Items[i]
107+
return statusMatches(conditionType, conditionStatus, item.Status.Conditions)
108+
}

cmd/flux/source.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,37 @@ func (a helmRepositoryListAdapter) asClientList() client.ObjectList {
195195
func (a helmRepositoryListAdapter) len() int {
196196
return len(a.HelmRepositoryList.Items)
197197
}
198+
199+
// sourcev1.ExternalArtifact
200+
201+
var externalArtifactType = apiType{
202+
kind: sourcev1.ExternalArtifactKind,
203+
humanKind: "source external-artifact",
204+
groupVersion: sourcev1.GroupVersion,
205+
}
206+
207+
type externalArtifactAdapter struct {
208+
*sourcev1.ExternalArtifact
209+
}
210+
211+
func (a externalArtifactAdapter) asClientObject() client.Object {
212+
return a.ExternalArtifact
213+
}
214+
215+
func (a externalArtifactAdapter) deepCopyClientObject() client.Object {
216+
return a.ExternalArtifact.DeepCopy()
217+
}
218+
219+
// sourcev1.ExternalArtifactList
220+
221+
type externalArtifactListAdapter struct {
222+
*sourcev1.ExternalArtifactList
223+
}
224+
225+
func (a externalArtifactListAdapter) asClientList() client.ObjectList {
226+
return a.ExternalArtifactList
227+
}
228+
229+
func (a externalArtifactListAdapter) len() int {
230+
return len(a.ExternalArtifactList.Items)
231+
}

0 commit comments

Comments
 (0)