Refactored and added pkg module

This commit is contained in:
Jean-Marc ANDRE 2020-12-12 10:21:27 +01:00
parent 9a3d9bd1a4
commit 93860e0172
9 changed files with 119 additions and 30 deletions

View File

@ -17,11 +17,11 @@ package cmd
import (
"github.com/spf13/cobra"
"github.com/desmo999r/formolcli/create"
"github.com/desmo999r/formolcli/pkg/backupsession"
)
// backupsessionCmd represents the backupsession command
var backupsessionCmd = &cobra.Command{
var createBackupsessionCmd = &cobra.Command{
Use: "backupsession",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
@ -33,12 +33,29 @@ to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
name, _ := cmd.Flags().GetString("name")
namespace, _ := cmd.Flags().GetString("namespace")
create.CreateBackupSession(name, namespace)
backupsession.CreateBackupSession(name, namespace)
},
}
var deleteBackupsessionCmd = &cobra.Command{
Use: "backupsession",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
name, _ := cmd.Flags().GetString("name")
namespace, _ := cmd.Flags().GetString("namespace")
backupsession.DeleteBackupSession(name, namespace)
},
}
func init() {
createCmd.AddCommand(backupsessionCmd)
createCmd.AddCommand(createBackupsessionCmd)
deleteCmd.AddCommand(deleteBackupsessionCmd)
// Here you will define your flags and configuration settings.
@ -49,8 +66,12 @@ func init() {
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// backupsessionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
backupsessionCmd.Flags().String("namespace", "", "The referenced BackupSessionConfiguration namespace")
backupsessionCmd.Flags().String("name", "", "The referenced BackupSessionConfiguration name")
backupsessionCmd.MarkFlagRequired("namespace")
backupsessionCmd.MarkFlagRequired("name")
createBackupsessionCmd.Flags().String("namespace", "", "The referenced BackupSessionConfiguration namespace")
createBackupsessionCmd.Flags().String("name", "", "The referenced BackupSessionConfiguration name")
createBackupsessionCmd.MarkFlagRequired("namespace")
createBackupsessionCmd.MarkFlagRequired("name")
deleteBackupsessionCmd.Flags().String("namespace", "", "The referenced BackupSessionConfiguration namespace")
deleteBackupsessionCmd.Flags().String("name", "", "The referenced BackupSessionConfiguration name")
deleteBackupsessionCmd.MarkFlagRequired("namespace")
deleteBackupsessionCmd.MarkFlagRequired("name")
}

51
src/cmd/delete.go Normal file
View File

@ -0,0 +1,51 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
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 cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// deleteCmd represents the delete command
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("delete called")
},
}
func init() {
rootCmd.AddCommand(deleteCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// deleteCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// deleteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View File

@ -19,7 +19,7 @@ import (
"fmt"
"github.com/spf13/cobra"
"github.com/desmo999r/formolcli/create"
"github.com/desmo999r/formolcli/pkg/server"
)
// serverCmd represents the server command
@ -34,7 +34,7 @@ This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("server called")
create.Server()
server.Server()
},
}

View File

@ -19,7 +19,7 @@ import (
"fmt"
"github.com/spf13/cobra"
"github.com/desmo999r/formolcli/backup"
"github.com/desmo999r/formolcli/pkg/backup"
)
// pvcCmd represents the pvc command

View File

@ -3,17 +3,17 @@ module github.com/desmo999r/formolcli
go 1.14
require (
github.com/desmo999r/formol v0.1.5-0.20201203082958-eb855f1c1202
github.com/desmo999r/formol v0.6.0
github.com/go-logr/logr v0.1.0
github.com/go-logr/zapr v0.1.0
github.com/mitchellh/go-homedir v1.1.0
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.8.1
github.com/onsi/ginkgo v1.12.1
github.com/onsi/gomega v1.10.1
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.3.2
go.uber.org/zap v1.10.0
k8s.io/api v0.17.2
k8s.io/apimachinery v0.17.2
k8s.io/client-go v0.17.2
sigs.k8s.io/controller-runtime v0.5.0
k8s.io/api v0.18.6
k8s.io/apimachinery v0.18.6
k8s.io/client-go v0.18.6
sigs.k8s.io/controller-runtime v0.6.4
)

View File

@ -1,4 +1,4 @@
package create
package backupsession
import (
"strings"
@ -16,12 +16,20 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
ctrl "sigs.k8s.io/controller-runtime"
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
"github.com/go-logr/logr"
)
func CreateBackupSession(name string, namespace string) {
log := zap.New(zap.UseDevMode(true)).WithName("CreateBackupSession")
ctrl.SetLogger(log)
log.V(0).Info("CreateBackupSession called")
var (
config *rest.Config
scheme *runtime.Scheme
cl client.Client
logger logr.Logger
)
func init() {
logger = zap.New(zap.UseDevMode(true))
log := logger.WithName("InitBackupSession")
ctrl.SetLogger(logger)
config, err := rest.InClusterConfig()
if err != nil {
config, err = clientcmd.BuildConfigFromFlags("", filepath.Join(os.Getenv("HOME"), ".kube", "config",))
@ -30,15 +38,23 @@ func CreateBackupSession(name string, namespace string) {
os.Exit(1)
}
}
scheme := runtime.NewScheme()
scheme = runtime.NewScheme()
_ = formolv1alpha1.AddToScheme(scheme)
_ = clientgoscheme.AddToScheme(scheme)
cl, err := client.New(config, client.Options{Scheme: scheme})
cl, err = client.New(config, client.Options{Scheme: scheme})
if err != nil {
log.Error(err, "unable to get client")
os.Exit(1)
}
}
func DeleteBackupSession(name string, namespace string) error {
return nil
}
func CreateBackupSession(name string, namespace string) {
log := logger.WithName("CreateBackupSession")
log.V(0).Info("CreateBackupSession called")
backupConfList := &formolv1alpha1.BackupConfigurationList{}
if err := cl.List(context.TODO(), backupConfList, client.InNamespace(namespace)); err != nil {
log.Error(err, "unable to get backupconf")

View File

@ -17,7 +17,7 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"github.com/desmo999r/formolcli/backup"
"github.com/desmo999r/formolcli/pkg/backup"
)
var (
@ -49,14 +49,14 @@ func init() {
panic("unable to get hostname")
}
pod, err := clientset.CoreV1().Pods(namespace).Get(hostname, metav1.GetOptions{})
pod, err := clientset.CoreV1().Pods(namespace).Get(context.Background(), hostname, metav1.GetOptions{})
if err != nil {
log.Error(err, "unable to get pod")
panic("unable to get pod")
}
podOwner := metav1.GetControllerOf(pod)
replicasetList, err := clientset.AppsV1().ReplicaSets(namespace).List(metav1.ListOptions{
replicasetList, err := clientset.AppsV1().ReplicaSets(namespace).List(context.Background(), metav1.ListOptions{
FieldSelector: "metadata.name=" + string(podOwner.Name),
})
if err != nil {

View File

@ -1,4 +1,4 @@
package create
package server
import (
"os"
@ -7,7 +7,7 @@ import (
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"github.com/desmo999r/formolcli/controllers"
"github.com/desmo999r/formolcli/pkg/controllers"
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
)
@ -39,6 +39,7 @@ func Server(){
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "12345.desmojim.fr",
Namespace: os.Getenv("POD_NAMESPACE"),
})
if err != nil {
setupLog.Error(err, "unable to create manager")