|
| 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 | +} |
0 commit comments