mirror of
				https://github.com/fluxcd/flux2.git
				synced 2025-10-31 08:17:19 +08:00 
			
		
		
		
	Refactor all remaining create, delete, export, get command to use adapter
Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
This commit is contained in:
		| @ -17,18 +17,10 @@ limitations under the License. | ||||
| package main | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"fmt" | ||||
|  | ||||
| 	sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" | ||||
| 	"github.com/spf13/cobra" | ||||
| 	corev1 "k8s.io/api/core/v1" | ||||
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||
| 	"k8s.io/apimachinery/pkg/types" | ||||
| 	"sigs.k8s.io/controller-runtime/pkg/client" | ||||
| 	"sigs.k8s.io/yaml" | ||||
|  | ||||
| 	"github.com/fluxcd/flux2/internal/utils" | ||||
| 	sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" | ||||
| ) | ||||
|  | ||||
| var exportSourceGitCmd = &cobra.Command{ | ||||
| @ -41,70 +33,17 @@ var exportSourceGitCmd = &cobra.Command{ | ||||
|   # Export a GitRepository source including the SSH key pair or basic auth credentials | ||||
|   flux export source git my-private-repo --with-credentials > source.yaml | ||||
| `, | ||||
| 	RunE: exportSourceGitCmdRun, | ||||
| 	RunE: exportWithSecretCommand{ | ||||
| 		object: gitRepositoryAdapter{&sourcev1.GitRepository{}}, | ||||
| 		list:   gitRepositoryListAdapter{&sourcev1.GitRepositoryList{}}, | ||||
| 	}.run, | ||||
| } | ||||
|  | ||||
| func init() { | ||||
| 	exportSourceCmd.AddCommand(exportSourceGitCmd) | ||||
| } | ||||
|  | ||||
| func exportSourceGitCmdRun(cmd *cobra.Command, args []string) error { | ||||
| 	if !exportArgs.all && len(args) < 1 { | ||||
| 		return fmt.Errorf("name is required") | ||||
| 	} | ||||
|  | ||||
| 	ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout) | ||||
| 	defer cancel() | ||||
|  | ||||
| 	kubeClient, err := utils.KubeClient(rootArgs.kubeconfig, rootArgs.kubecontext) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if exportArgs.all { | ||||
| 		var list sourcev1.GitRepositoryList | ||||
| 		err = kubeClient.List(ctx, &list, client.InNamespace(rootArgs.namespace)) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 		if len(list.Items) == 0 { | ||||
| 			logger.Failuref("no source found in %s namespace", rootArgs.namespace) | ||||
| 			return nil | ||||
| 		} | ||||
|  | ||||
| 		for _, repository := range list.Items { | ||||
| 			if err := exportGit(repository); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			if exportSourceWithCred { | ||||
| 				if err := exportGitCredentials(ctx, kubeClient, repository); err != nil { | ||||
| 					return err | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 	} else { | ||||
| 		name := args[0] | ||||
| 		namespacedName := types.NamespacedName{ | ||||
| 			Namespace: rootArgs.namespace, | ||||
| 			Name:      name, | ||||
| 		} | ||||
| 		var repository sourcev1.GitRepository | ||||
| 		err = kubeClient.Get(ctx, namespacedName, &repository) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		if err := exportGit(repository); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		if exportSourceWithCred { | ||||
| 			return exportGitCredentials(ctx, kubeClient, repository) | ||||
| 		} | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func exportGit(source sourcev1.GitRepository) error { | ||||
| func exportGit(source *sourcev1.GitRepository) interface{} { | ||||
| 	gvk := sourcev1.GroupVersion.WithKind(sourcev1.GitRepositoryKind) | ||||
| 	export := sourcev1.GitRepository{ | ||||
| 		TypeMeta: metav1.TypeMeta{ | ||||
| @ -120,48 +59,33 @@ func exportGit(source sourcev1.GitRepository) error { | ||||
| 		Spec: source.Spec, | ||||
| 	} | ||||
|  | ||||
| 	data, err := yaml.Marshal(export) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	fmt.Println("---") | ||||
| 	fmt.Println(resourceToString(data)) | ||||
| 	return nil | ||||
| 	return export | ||||
| } | ||||
|  | ||||
| func exportGitCredentials(ctx context.Context, kubeClient client.Client, source sourcev1.GitRepository) error { | ||||
| func getGitSecret(source *sourcev1.GitRepository) *types.NamespacedName { | ||||
| 	if source.Spec.SecretRef != nil { | ||||
| 		namespacedName := types.NamespacedName{ | ||||
| 			Namespace: source.Namespace, | ||||
| 			Name:      source.Spec.SecretRef.Name, | ||||
| 		} | ||||
| 		var cred corev1.Secret | ||||
| 		err := kubeClient.Get(ctx, namespacedName, &cred) | ||||
| 		if err != nil { | ||||
| 			return fmt.Errorf("failed to retrieve secret %s, error: %w", namespacedName.Name, err) | ||||
| 		} | ||||
|  | ||||
| 		exported := corev1.Secret{ | ||||
| 			TypeMeta: metav1.TypeMeta{ | ||||
| 				APIVersion: "v1", | ||||
| 				Kind:       "Secret", | ||||
| 			}, | ||||
| 			ObjectMeta: metav1.ObjectMeta{ | ||||
| 				Name:      namespacedName.Name, | ||||
| 				Namespace: namespacedName.Namespace, | ||||
| 			}, | ||||
| 			Data: cred.Data, | ||||
| 			Type: cred.Type, | ||||
| 		} | ||||
|  | ||||
| 		data, err := yaml.Marshal(exported) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 		fmt.Println("---") | ||||
| 		fmt.Println(resourceToString(data)) | ||||
| 		return &namespacedName | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (ex gitRepositoryAdapter) secret() *types.NamespacedName { | ||||
| 	return getGitSecret(ex.GitRepository) | ||||
| } | ||||
|  | ||||
| func (ex gitRepositoryListAdapter) secretItem(i int) *types.NamespacedName { | ||||
| 	return getGitSecret(&ex.GitRepositoryList.Items[i]) | ||||
| } | ||||
|  | ||||
| func (ex gitRepositoryAdapter) export() interface{} { | ||||
| 	return exportGit(ex.GitRepository) | ||||
| } | ||||
|  | ||||
| func (ex gitRepositoryListAdapter) exportItem(i int) interface{} { | ||||
| 	return exportGit(&ex.GitRepositoryList.Items[i]) | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Somtochi Onyekwere
					Somtochi Onyekwere