@ -17,16 +17,10 @@ limitations under the License.
package main
import (
"context"
"fmt"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
"k8s.io/apimachinery/pkg/runtime"
"github.com/fluxcd/flux2/internal/utils"
imagev1 "github.com/fluxcd/image-reflector-controller/api/v1alpha1"
)
@ -40,60 +34,19 @@ var exportImagePolicyCmd = &cobra.Command{
# Export a specific policy
flux export auto image - policy alpine1x > alpine1x . yaml
` ,
RunE : exportImagePolicyRun ,
RunE : exportCommand {
object : exportableImagePolicy { & imagev1 . ImagePolicy { } } ,
list : exportableImagePolicyList { & imagev1 . ImagePolicyList { } } ,
} . run ,
}
func init ( ) {
exportAutoCmd . AddCommand ( exportImagePolicyCmd )
}
func exportImagePolicyRun ( cmd * cobra . Command , args [ ] string ) error {
if ! exportAll && len ( args ) < 1 {
return fmt . Errorf ( "name is required" )
}
ctx , cancel := context . WithTimeout ( context . Background ( ) , timeout )
defer cancel ( )
kubeClient , err := utils . KubeClient ( kubeconfig , kubecontext )
if err != nil {
return err
}
if exportAll {
var list imagev1 . ImagePolicyList
err = kubeClient . List ( ctx , & list , client . InNamespace ( namespace ) )
if err != nil {
return err
}
if len ( list . Items ) == 0 {
logger . Failuref ( "no imagepolicy objects found in %s namespace" , namespace )
return nil
}
for _ , policy := range list . Items {
if err := exportImagePolicy ( policy ) ; err != nil {
return err
}
}
} else {
name := args [ 0 ]
namespacedName := types . NamespacedName {
Namespace : namespace ,
Name : name ,
}
var policy imagev1 . ImagePolicy
err = kubeClient . Get ( ctx , namespacedName , & policy )
if err != nil {
return err
}
return exportImagePolicy ( policy )
}
return nil
}
func exportImagePolicy ( policy imagev1 . ImagePolicy ) error {
// Export returns a ImagePolicy value which has extraneous information
// stripped out.
func exportImagePolicy ( item * imagev1 . ImagePolicy ) interface { } {
gvk := imagev1 . GroupVersion . WithKind ( imagev1 . ImagePolicyKind )
export := imagev1 . ImagePolicy {
TypeMeta : metav1 . TypeMeta {
@ -101,20 +54,40 @@ func exportImagePolicy(policy imagev1.ImagePolicy) error {
APIVersion : gvk . GroupVersion ( ) . String ( ) ,
} ,
ObjectMeta : metav1 . ObjectMeta {
Name : policy . Name ,
Namespace : policy . Namespace ,
Labels : policy . Labels ,
Annotations : policy . Annotations ,
Name : item . Name ,
Namespace : item . Namespace ,
Labels : item . Labels ,
Annotations : item . Annotations ,
} ,
Spec : policy . Spec ,
Spec : item . Spec ,
}
return export
}
data , err := yaml . Marshal ( export )
if err != nil {
return err
}
type exportableImagePolicy struct {
policy * imagev1 . ImagePolicy
}
func ( ex exportableImagePolicy ) AsClientObject ( ) runtime . Object {
return ex . policy
}
func ( ex exportableImagePolicy ) Export ( ) interface { } {
return exportImagePolicy ( ex . policy )
}
type exportableImagePolicyList struct {
list * imagev1 . ImagePolicyList
}
func ( ex exportableImagePolicyList ) AsClientObject ( ) runtime . Object {
return ex . list
}
func ( ex exportableImagePolicyList ) Len ( ) int {
return len ( ex . list . Items )
}
fmt . Println ( "---" )
fmt . Println ( resourceToString ( data ) )
return nil
func ( ex exportableImagePolicyList ) ExportAt ( i int ) interface { } {
return exportImagePolicy ( & ex . list . Items [ i ] )
}