mirror of https://github.com/fluxcd/flux2.git
Merge pull request #5520 from fluxcd/artifact-generator
Add read-only commands for `ArtifactGenerator` kindpull/5521/head
commit
9caea521ea
@ -0,0 +1,57 @@
|
||||
/*
|
||||
Copyright 2025 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 main
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
|
||||
swapi "github.com/fluxcd/source-watcher/api/v2/v1beta1"
|
||||
)
|
||||
|
||||
// swapi.ArtifactGenerator
|
||||
|
||||
var artifactGeneratorType = apiType{
|
||||
kind: swapi.ArtifactGeneratorKind,
|
||||
humanKind: "artifactgenerator",
|
||||
groupVersion: swapi.GroupVersion,
|
||||
}
|
||||
|
||||
type artifactGeneratorAdapter struct {
|
||||
*swapi.ArtifactGenerator
|
||||
}
|
||||
|
||||
func (h artifactGeneratorAdapter) asClientObject() client.Object {
|
||||
return h.ArtifactGenerator
|
||||
}
|
||||
|
||||
func (h artifactGeneratorAdapter) deepCopyClientObject() client.Object {
|
||||
return h.ArtifactGenerator.DeepCopy()
|
||||
}
|
||||
|
||||
// swapi.ArtifactGeneratorList
|
||||
|
||||
type artifactGeneratorListAdapter struct {
|
||||
*swapi.ArtifactGeneratorList
|
||||
}
|
||||
|
||||
func (h artifactGeneratorListAdapter) asClientList() client.ObjectList {
|
||||
return h.ArtifactGeneratorList
|
||||
}
|
||||
|
||||
func (h artifactGeneratorListAdapter) len() int {
|
||||
return len(h.ArtifactGeneratorList.Items)
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
Copyright 2025 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 main
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var exportArtifactCmd = &cobra.Command{
|
||||
Use: "artifact",
|
||||
Short: "Export artifact objects",
|
||||
Long: `The export artifact sub-commands export artifacts objects in YAML format.`,
|
||||
}
|
||||
|
||||
func init() {
|
||||
exportCmd.AddCommand(exportArtifactCmd)
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
/*
|
||||
Copyright 2025 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 main
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
swapi "github.com/fluxcd/source-watcher/api/v2/v1beta1"
|
||||
)
|
||||
|
||||
var exportArtifactGeneratorCmd = &cobra.Command{
|
||||
Use: "generator [name]",
|
||||
Short: "Export ArtifactGenerator resources in YAML format",
|
||||
Long: "The export artifact generator command exports one or all ArtifactGenerator resources in YAML format.",
|
||||
Example: ` # Export all ArtifactGenerator resources
|
||||
flux export artifact generator --all > artifact-generators.yaml
|
||||
|
||||
# Export a specific generator
|
||||
flux export artifact generator my-generator > my-generator.yaml`,
|
||||
ValidArgsFunction: resourceNamesCompletionFunc(swapi.GroupVersion.WithKind(swapi.ArtifactGeneratorKind)),
|
||||
RunE: exportCommand{
|
||||
object: artifactGeneratorAdapter{&swapi.ArtifactGenerator{}},
|
||||
list: artifactGeneratorListAdapter{&swapi.ArtifactGeneratorList{}},
|
||||
}.run,
|
||||
}
|
||||
|
||||
func init() {
|
||||
exportArtifactCmd.AddCommand(exportArtifactGeneratorCmd)
|
||||
}
|
||||
|
||||
// Export returns an ArtifactGenerator value which has
|
||||
// extraneous information stripped out.
|
||||
func exportArtifactGenerator(item *swapi.ArtifactGenerator) interface{} {
|
||||
gvk := swapi.GroupVersion.WithKind(swapi.ArtifactGeneratorKind)
|
||||
export := swapi.ArtifactGenerator{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: gvk.Kind,
|
||||
APIVersion: gvk.GroupVersion().String(),
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: item.Name,
|
||||
Namespace: item.Namespace,
|
||||
Labels: item.Labels,
|
||||
Annotations: item.Annotations,
|
||||
},
|
||||
Spec: item.Spec,
|
||||
}
|
||||
return export
|
||||
}
|
||||
|
||||
func (ex artifactGeneratorAdapter) export() interface{} {
|
||||
return exportArtifactGenerator(ex.ArtifactGenerator)
|
||||
}
|
||||
|
||||
func (ex artifactGeneratorListAdapter) exportItem(i int) interface{} {
|
||||
return exportArtifactGenerator(&ex.ArtifactGeneratorList.Items[i])
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
Copyright 2025 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 main
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var getArtifactCmd = &cobra.Command{
|
||||
Use: "artifacts",
|
||||
Aliases: []string{"artifact"},
|
||||
Short: "Get artifact object status",
|
||||
Long: `The get artifact sub-commands print the status of artifact objects.`,
|
||||
}
|
||||
|
||||
func init() {
|
||||
getCmd.AddCommand(getArtifactCmd)
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
/*
|
||||
Copyright 2025 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 main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
|
||||
swapi "github.com/fluxcd/source-watcher/api/v2/v1beta1"
|
||||
)
|
||||
|
||||
var getArtifactGeneratorCmd = &cobra.Command{
|
||||
Use: "generators",
|
||||
Aliases: []string{"generator"},
|
||||
Short: "Get artifact generator statuses",
|
||||
Long: `The get artifact generator command prints the statuses of the resources.`,
|
||||
Example: ` # List all ArtifactGenerators and their status
|
||||
flux get artifact generators`,
|
||||
ValidArgsFunction: resourceNamesCompletionFunc(swapi.GroupVersion.WithKind(swapi.ArtifactGeneratorKind)),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
get := getCommand{
|
||||
apiType: receiverType,
|
||||
list: artifactGeneratorListAdapter{&swapi.ArtifactGeneratorList{}},
|
||||
funcMap: make(typeMap),
|
||||
}
|
||||
|
||||
err := get.funcMap.registerCommand(get.apiType.kind, func(obj runtime.Object) (summarisable, error) {
|
||||
o, ok := obj.(*swapi.ArtifactGenerator)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("impossible to cast type %#v generator", obj)
|
||||
}
|
||||
|
||||
sink := artifactGeneratorListAdapter{&swapi.ArtifactGeneratorList{
|
||||
Items: []swapi.ArtifactGenerator{
|
||||
*o,
|
||||
}}}
|
||||
return sink, nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := get.run(cmd, args); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
getArtifactCmd.AddCommand(getArtifactGeneratorCmd)
|
||||
}
|
||||
|
||||
func (s artifactGeneratorListAdapter) summariseItem(i int, includeNamespace bool, includeKind bool) []string {
|
||||
item := s.Items[i]
|
||||
status, msg := statusAndMessage(item.Status.Conditions)
|
||||
return append(nameColumns(&item, includeNamespace, includeKind),
|
||||
cases.Title(language.English).String(strconv.FormatBool(item.IsDisabled())), status, msg)
|
||||
}
|
||||
|
||||
func (s artifactGeneratorListAdapter) headers(includeNamespace bool) []string {
|
||||
headers := []string{"Name", "Suspended", "Ready", "Message"}
|
||||
if includeNamespace {
|
||||
return append(namespaceHeader, headers...)
|
||||
}
|
||||
return headers
|
||||
}
|
||||
|
||||
func (s artifactGeneratorListAdapter) statusSelectorMatches(i int, conditionType, conditionStatus string) bool {
|
||||
item := s.Items[i]
|
||||
return statusMatches(conditionType, conditionStatus, item.Status.Conditions)
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
Copyright 2025 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 main
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var treeArtifactCmd = &cobra.Command{
|
||||
Use: "artifact",
|
||||
Short: "Print artifact objects reconciled by Flux",
|
||||
Long: `The tree artifact sub-commands print a list of artifact objects.`,
|
||||
}
|
||||
|
||||
func init() {
|
||||
treeCmd.AddCommand(treeArtifactCmd)
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
/*
|
||||
Copyright 2025 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 main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
"github.com/fluxcd/cli-utils/pkg/object"
|
||||
sourcev1 "github.com/fluxcd/source-controller/api/v1"
|
||||
swapi "github.com/fluxcd/source-watcher/api/v2/v1beta1"
|
||||
|
||||
"github.com/fluxcd/flux2/v2/internal/tree"
|
||||
"github.com/fluxcd/flux2/v2/internal/utils"
|
||||
)
|
||||
|
||||
var treeArtifactGeneratorCmd = &cobra.Command{
|
||||
Use: "generator [name]",
|
||||
Short: "Print the inventory of an ArtifactGenerator",
|
||||
Long: withPreviewNote(`The tree command prints the ExternalArtifact list managed by an ArtifactGenerator.'`),
|
||||
Example: ` # Print the ExternalArtifacts managed by an ArtifactGenerator
|
||||
flux tree artifact generator my-generator`,
|
||||
RunE: treeArtifactGeneratorCmdRun,
|
||||
Args: cobra.ExactArgs(1),
|
||||
ValidArgsFunction: resourceNamesCompletionFunc(swapi.GroupVersion.WithKind(swapi.ArtifactGeneratorKind)),
|
||||
}
|
||||
|
||||
type TreeArtifactGeneratorFlags struct {
|
||||
output string
|
||||
}
|
||||
|
||||
var treeArtifactGeneratorArgs TreeArtifactGeneratorFlags
|
||||
|
||||
func init() {
|
||||
treeArtifactGeneratorCmd.Flags().StringVarP(&treeArtifactGeneratorArgs.output, "output", "o", "",
|
||||
"the format in which the tree should be printed. can be 'json' or 'yaml'")
|
||||
treeArtifactCmd.AddCommand(treeArtifactGeneratorCmd)
|
||||
}
|
||||
|
||||
func treeArtifactGeneratorCmdRun(cmd *cobra.Command, args []string) error {
|
||||
if len(args) < 1 {
|
||||
return fmt.Errorf("generator name is required")
|
||||
}
|
||||
name := args[0]
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
|
||||
defer cancel()
|
||||
|
||||
kubeClient, err := utils.KubeClient(kubeconfigArgs, kubeclientOptions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ag := &swapi.ArtifactGenerator{}
|
||||
err = kubeClient.Get(ctx, client.ObjectKey{
|
||||
Namespace: *kubeconfigArgs.Namespace,
|
||||
Name: name,
|
||||
}, ag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
kTree := tree.New(object.ObjMetadata{
|
||||
Namespace: ag.Namespace,
|
||||
Name: ag.Name,
|
||||
GroupKind: schema.GroupKind{Group: swapi.GroupVersion.Group, Kind: swapi.ArtifactGeneratorKind},
|
||||
})
|
||||
|
||||
for _, ea := range ag.Status.Inventory {
|
||||
kTree.Add(object.ObjMetadata{
|
||||
Namespace: ea.Namespace,
|
||||
Name: ea.Name,
|
||||
GroupKind: schema.GroupKind{Group: sourcev1.GroupVersion.Group, Kind: sourcev1.ExternalArtifactKind},
|
||||
})
|
||||
}
|
||||
|
||||
switch treeArtifactGeneratorArgs.output {
|
||||
case "json":
|
||||
data, err := json.MarshalIndent(kTree, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rootCmd.Println(string(data))
|
||||
case "yaml":
|
||||
data, err := yaml.Marshal(kTree)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rootCmd.Println(string(data))
|
||||
default:
|
||||
rootCmd.Println(kTree.Print())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue