From 0f298a60ac450def5f4258ef4e11861420071286 Mon Sep 17 00:00:00 2001 From: Jason Caylor Date: Mon, 6 Nov 2023 23:09:56 -0500 Subject: [PATCH 1/2] updating the hobbyfarmv1 client and adding namespace params to api call functions --- go.mod | 8 +++----- go.sum | 4 ++-- pkg/cmd/create.go | 2 +- pkg/cmd/delete.go | 2 +- pkg/cmd/get.go | 2 +- pkg/cmd/info.go | 4 ++-- pkg/cmd/root.go | 5 +++-- pkg/info/email.go | 46 +++++++++++++++++++++++-------------------- pkg/scenario/apply.go | 18 ++++++++--------- 9 files changed, 47 insertions(+), 44 deletions(-) diff --git a/go.mod b/go.mod index a2c1e5a..91e49d1 100644 --- a/go.mod +++ b/go.mod @@ -2,15 +2,13 @@ module github.com/hobbyfarm/hfcli go 1.16 -replace ( - github.com/hobbyfarm/gargantua => github.com/hobbyfarm/gargantua v0.2.2-0.20210823170529-e2466136c002 - k8s.io/client-go => k8s.io/client-go v0.20.0 -) +// github.com/hobbyfarm/gargantua => github.com/hobbyfarm/gargantua v0.2.2-0.20210823170529-e2466136c002 +replace k8s.io/client-go => k8s.io/client-go v0.20.0 require ( github.com/BurntSushi/toml v0.3.1 github.com/ghodss/yaml v1.0.0 - github.com/hobbyfarm/gargantua v0.2.0 + github.com/hobbyfarm/gargantua v1.0.0 github.com/rancher/wrangler-cli v0.0.0-20210217230406-95cfa275f52f github.com/sirupsen/logrus v1.6.0 github.com/spf13/cobra v1.2.1 diff --git a/go.sum b/go.sum index 434b2ff..1daf569 100644 --- a/go.sum +++ b/go.sum @@ -316,8 +316,8 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= -github.com/hobbyfarm/gargantua v0.2.2-0.20210823170529-e2466136c002 h1:i0sxD0unP5DQ0KL6hckBO4loTW8qwPHZD0tKKUzqYXI= -github.com/hobbyfarm/gargantua v0.2.2-0.20210823170529-e2466136c002/go.mod h1:zcyT56Ifb6JweVRYxdykUIanIwBuSFq9vs+UHWoFc/4= +github.com/hobbyfarm/gargantua v1.0.0 h1:xcFOx1OTvf91OV+fbGoBtr5uH52dGLr9QVFfZYgSTuY= +github.com/hobbyfarm/gargantua v1.0.0/go.mod h1:zcyT56Ifb6JweVRYxdykUIanIwBuSFq9vs+UHWoFc/4= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= diff --git a/pkg/cmd/create.go b/pkg/cmd/create.go index 38c274b..ef87c54 100644 --- a/pkg/cmd/create.go +++ b/pkg/cmd/create.go @@ -55,5 +55,5 @@ func (sc *Scenario) Run(cmd *cobra.Command, args []string) error { return err } - return scenario.Apply(s, HfClient) + return scenario.Apply(s, Namespace, HfClient) } diff --git a/pkg/cmd/delete.go b/pkg/cmd/delete.go index bc23b62..821545b 100644 --- a/pkg/cmd/delete.go +++ b/pkg/cmd/delete.go @@ -40,5 +40,5 @@ func NewDeleteScenario() *cobra.Command { func (sc *DeleteScenario) Run(cmd *cobra.Command, args []string) error { logrus.Info(args[0]) - return scenario.Delete(args[0], HfClient) + return scenario.Delete(args[0], Namespace, HfClient) } diff --git a/pkg/cmd/get.go b/pkg/cmd/get.go index 72b892c..04bae00 100644 --- a/pkg/cmd/get.go +++ b/pkg/cmd/get.go @@ -40,7 +40,7 @@ func NewGetScenario() *cobra.Command { func (sc *GetScenario) Run(cmd *cobra.Command, args []string) error { logrus.Info(args[0], args[1]) - s, err := scenario.Get(args[0], HfClient) + s, err := scenario.Get(args[0], Namespace, HfClient) if err != nil { return err diff --git a/pkg/cmd/info.go b/pkg/cmd/info.go index 553708e..dd576af 100644 --- a/pkg/cmd/info.go +++ b/pkg/cmd/info.go @@ -43,7 +43,7 @@ func (ie *InfoEmail) Run(cmd *cobra.Command, args []string) error { return cmd.Help() } - return info.GetEmail(args[0], HfClient) + return info.GetEmail(args[0], Namespace, HfClient) } type InfoAccessCode struct { @@ -64,5 +64,5 @@ func (iac *InfoAccessCode) Run(cmd *cobra.Command, args []string) error { return cmd.Help() } - return info.GetAccessCode(args[0], HfClient, iac.Stats) + return info.GetAccessCode(args[0], Namespace, HfClient, iac.Stats) } diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index 60b953e..39e5db2 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -1,7 +1,8 @@ package cmd import ( - hfClientSet "github.com/hobbyfarm/gargantua/pkg/client/clientset/versioned/typed/hobbyfarm.io/v1" + // hfClientSet "github.com/hobbyfarm/gargantua/pkg/client/clientset/versioned/typed/hobbyfarm.io/v1" + hfClientSet "github.com/hobbyfarm/gargantua/pkg/client/clientset/versioned" command "github.com/rancher/wrangler-cli" "github.com/spf13/cobra" "k8s.io/client-go/tools/clientcmd" @@ -15,7 +16,7 @@ type Hfcli struct { var ( Namespace string - HfClient *hfClientSet.HobbyfarmV1Client + HfClient *hfClientSet.Clientset ) func App() *cobra.Command { diff --git a/pkg/info/email.go b/pkg/info/email.go index f646ad7..364f422 100644 --- a/pkg/info/email.go +++ b/pkg/info/email.go @@ -3,27 +3,31 @@ package info import ( "context" "fmt" + hf "github.com/hobbyfarm/gargantua/pkg/apis/hobbyfarm.io/v1" - hfClientSet "github.com/hobbyfarm/gargantua/pkg/client/clientset/versioned/typed/hobbyfarm.io/v1" + + // hfClientSet "github.com/hobbyfarm/gargantua/pkg/client/clientset/versioned/typed/hobbyfarm.io/v1" + "os" + "text/tabwriter" + + hfClientSet "github.com/hobbyfarm/gargantua/pkg/client/clientset/versioned" "github.com/sirupsen/logrus" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "os" - "text/tabwriter" ) type SessionDetails struct { SessionVMMap map[string][]hf.VirtualMachine } -func GetEmail(email string, hfc *hfClientSet.HobbyfarmV1Client) (err error) { +func GetEmail(email string, Namespace string, hfc *hfClientSet.Clientset) (err error) { - userid, err := getUser(email, hfc) + userid, err := getUser(email, Namespace, hfc) if err != nil { return err } - sDetails, err := getUserAllocatedVMs(userid, hfc) + sDetails, err := getUserAllocatedVMs(userid, Namespace, hfc) if err != nil { return err } @@ -31,9 +35,9 @@ func GetEmail(email string, hfc *hfClientSet.HobbyfarmV1Client) (err error) { return printReport([]SessionDetails{*sDetails}) } -func GetAccessCode(accesscode string, hfc *hfClientSet.HobbyfarmV1Client, stats bool) (err error) { +func GetAccessCode(accesscode string, Namespace string, hfc *hfClientSet.Clientset, stats bool) (err error) { - seList, err := hfc.ScheduledEvents().List(context.TODO(), metav1.ListOptions{}) + seList, err := hfc.HobbyfarmV1().ScheduledEvents(Namespace).List(context.TODO(), metav1.ListOptions{}) if err != nil { if apierrors.IsNotFound(err) { @@ -66,14 +70,14 @@ func GetAccessCode(accesscode string, hfc *hfClientSet.HobbyfarmV1Client, stats } logrus.Infof("scheduled event %s has accesscode %s", seEvents[0].Spec.Name, accesscode) - userIDS, err := getAllUsers(accesscode, hfc) + userIDS, err := getAllUsers(accesscode, Namespace, hfc) if err != nil { return err } var sDetails []SessionDetails for _, user := range userIDS { - s, err := getUserAllocatedVMs(user, hfc) + s, err := getUserAllocatedVMs(user, Namespace, hfc) if err != nil { return err } @@ -86,8 +90,8 @@ func GetAccessCode(accesscode string, hfc *hfClientSet.HobbyfarmV1Client, stats return printReport(sDetails) } -func getUser(email string, hfc *hfClientSet.HobbyfarmV1Client) (userid string, err error) { - users, err := hfc.Users().List(context.TODO(), metav1.ListOptions{}) +func getUser(email string, Namespace string, hfc *hfClientSet.Clientset) (userid string, err error) { + users, err := hfc.HobbyfarmV1().Users(Namespace).List(context.TODO(), metav1.ListOptions{}) if err != nil { if apierrors.IsNotFound(err) { return userid, fmt.Errorf("no users found in cluster") @@ -106,8 +110,8 @@ func getUser(email string, hfc *hfClientSet.HobbyfarmV1Client) (userid string, e return userid, err } -func getAllUsers(accesscode string, hfc *hfClientSet.HobbyfarmV1Client) (userIDS []string, err error) { - userList, err := hfc.Users().List(context.TODO(), metav1.ListOptions{}) +func getAllUsers(accesscode string, Namespace string, hfc *hfClientSet.Clientset) (userIDS []string, err error) { + userList, err := hfc.HobbyfarmV1().Users(Namespace).List(context.TODO(), metav1.ListOptions{}) if err != nil { if apierrors.IsNotFound(err) { @@ -127,8 +131,8 @@ func getAllUsers(accesscode string, hfc *hfClientSet.HobbyfarmV1Client) (userIDS return userIDS, nil } -func getUserAllocatedVMs(userID string, hfc *hfClientSet.HobbyfarmV1Client) (sDetails *SessionDetails, err error) { - sessionList, err := hfc.Sessions().List(context.TODO(), metav1.ListOptions{}) +func getUserAllocatedVMs(userID string, Namespace string, hfc *hfClientSet.Clientset) (sDetails *SessionDetails, err error) { + sessionList, err := hfc.HobbyfarmV1().Sessions(Namespace).List(context.TODO(), metav1.ListOptions{}) if err != nil { if apierrors.IsNotFound(err) { return nil, fmt.Errorf("no active sessions found for the user") @@ -151,7 +155,7 @@ func getUserAllocatedVMs(userID string, hfc *hfClientSet.HobbyfarmV1Client) (sDe for session, vmclaims := range tmpStore { var vmList []hf.VirtualMachine for _, vmc := range vmclaims { - vml, err := getVirtualMachinesForVMC(vmc, hfc) + vml, err := getVirtualMachinesForVMC(vmc, Namespace, hfc) if err != nil { return nil, err } @@ -163,8 +167,8 @@ func getUserAllocatedVMs(userID string, hfc *hfClientSet.HobbyfarmV1Client) (sDe return sDetails, nil } -func getVirtualMachinesForVMC(vmc string, hfc *hfClientSet.HobbyfarmV1Client) (vmList []hf.VirtualMachine, err error) { - vmClaim, err := hfc.VirtualMachineClaims().Get(context.TODO(), vmc, metav1.GetOptions{}) +func getVirtualMachinesForVMC(vmc string, Namespace string, hfc *hfClientSet.Clientset) (vmList []hf.VirtualMachine, err error) { + vmClaim, err := hfc.HobbyfarmV1().VirtualMachineClaims(Namespace).Get(context.TODO(), vmc, metav1.GetOptions{}) if err != nil { if apierrors.IsNotFound(err) { // object has been probably deleted.. no need to check @@ -176,7 +180,7 @@ func getVirtualMachinesForVMC(vmc string, hfc *hfClientSet.HobbyfarmV1Client) (v dbr := vmClaim.Status.DynamicBindRequestId - dbClaim, err := hfc.DynamicBindRequests().Get(context.TODO(), dbr, metav1.GetOptions{}) + dbClaim, err := hfc.HobbyfarmV1().DynamicBindRequests(Namespace).Get(context.TODO(), dbr, metav1.GetOptions{}) if err != nil { if apierrors.IsNotFound(err) { return vmList, nil @@ -186,7 +190,7 @@ func getVirtualMachinesForVMC(vmc string, hfc *hfClientSet.HobbyfarmV1Client) (v } for _, vm := range dbClaim.Status.VirtualMachineIds { - vmInfo, err := hfc.VirtualMachines().Get(context.TODO(), vm, metav1.GetOptions{}) + vmInfo, err := hfc.HobbyfarmV1().VirtualMachines(Namespace).Get(context.TODO(), vm, metav1.GetOptions{}) if err != nil { if apierrors.IsNotFound(err) { continue diff --git a/pkg/scenario/apply.go b/pkg/scenario/apply.go index 2970445..a8b6c66 100644 --- a/pkg/scenario/apply.go +++ b/pkg/scenario/apply.go @@ -5,21 +5,21 @@ import ( "fmt" hf "github.com/hobbyfarm/gargantua/pkg/apis/hobbyfarm.io/v1" - hfClientSet "github.com/hobbyfarm/gargantua/pkg/client/clientset/versioned/typed/hobbyfarm.io/v1" + hfClientSet "github.com/hobbyfarm/gargantua/pkg/client/clientset/versioned" "github.com/sirupsen/logrus" apierrors "k8s.io/apimachinery/pkg/api/errors" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -func Apply(s *hf.Scenario, hfc *hfClientSet.HobbyfarmV1Client) (err error) { +func Apply(s *hf.Scenario, Namespace string, hfc *hfClientSet.Clientset) (err error) { // check if scneario exists // - sGet, err := hfc.Scenarios().Get(context.TODO(), s.GetName(), v1.GetOptions{}) + sGet, err := hfc.HobbyfarmV1().Scenarios("Hobbyfarm123").Get(context.TODO(), s.GetName(), v1.GetOptions{}) if err != nil { if apierrors.IsNotFound(err) { logrus.Infof("creating scenario %s", s.GetName()) - _, err = hfc.Scenarios().Create(context.TODO(), s, v1.CreateOptions{}) + _, err = hfc.HobbyfarmV1().Scenarios("Hobbyfarm123").Create(context.TODO(), s, v1.CreateOptions{}) return err } else { return err @@ -31,7 +31,7 @@ func Apply(s *hf.Scenario, hfc *hfClientSet.HobbyfarmV1Client) (err error) { if ok && key == "hfcli" { s.ObjectMeta.ResourceVersion = sGet.ObjectMeta.GetResourceVersion() logrus.Infof("updating scenario %s", s.GetName()) - _, err = hfc.Scenarios().Update(context.TODO(), s, v1.UpdateOptions{}) + _, err = hfc.HobbyfarmV1().Scenarios("Hobbyfarm123").Update(context.TODO(), s, v1.UpdateOptions{}) } else { err = fmt.Errorf("scenario %s already exists and is not managed by hfcli", sGet.GetName()) } @@ -41,14 +41,14 @@ func Apply(s *hf.Scenario, hfc *hfClientSet.HobbyfarmV1Client) (err error) { return err } -func Get(name string, hfc *hfClientSet.HobbyfarmV1Client) (s *hf.Scenario, err error) { +func Get(name string, Namespace string, hfc *hfClientSet.Clientset) (s *hf.Scenario, err error) { logrus.Infof("downloading scenario %s", name) - return hfc.Scenarios().Get(context.TODO(), name, v1.GetOptions{}) + return hfc.HobbyfarmV1().Scenarios("Hobbyfarm123").Get(context.TODO(), name, v1.GetOptions{}) } -func Delete(name string, hfc *hfClientSet.HobbyfarmV1Client) (err error) { +func Delete(name string, Namespace string, hfc *hfClientSet.Clientset) (err error) { logrus.Infof("deleting scenario %s", name) - return hfc.Scenarios().Delete(context.TODO(), name, v1.DeleteOptions{}) + return hfc.HobbyfarmV1().Scenarios("Hobbyfarm123").Delete(context.TODO(), name, v1.DeleteOptions{}) } From 04395f611ba01c11630d52e496c7cfc15e01f7d9 Mon Sep 17 00:00:00 2001 From: Jason Caylor Date: Tue, 7 Nov 2023 08:32:09 -0500 Subject: [PATCH 2/2] removing hard-coded namespace input to scenario functionality, leftover from testing --- pkg/scenario/apply.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/scenario/apply.go b/pkg/scenario/apply.go index a8b6c66..08eb138 100644 --- a/pkg/scenario/apply.go +++ b/pkg/scenario/apply.go @@ -14,12 +14,12 @@ import ( func Apply(s *hf.Scenario, Namespace string, hfc *hfClientSet.Clientset) (err error) { // check if scneario exists // - sGet, err := hfc.HobbyfarmV1().Scenarios("Hobbyfarm123").Get(context.TODO(), s.GetName(), v1.GetOptions{}) + sGet, err := hfc.HobbyfarmV1().Scenarios(Namespace).Get(context.TODO(), s.GetName(), v1.GetOptions{}) if err != nil { if apierrors.IsNotFound(err) { logrus.Infof("creating scenario %s", s.GetName()) - _, err = hfc.HobbyfarmV1().Scenarios("Hobbyfarm123").Create(context.TODO(), s, v1.CreateOptions{}) + _, err = hfc.HobbyfarmV1().Scenarios(Namespace).Create(context.TODO(), s, v1.CreateOptions{}) return err } else { return err @@ -31,7 +31,7 @@ func Apply(s *hf.Scenario, Namespace string, hfc *hfClientSet.Clientset) (err er if ok && key == "hfcli" { s.ObjectMeta.ResourceVersion = sGet.ObjectMeta.GetResourceVersion() logrus.Infof("updating scenario %s", s.GetName()) - _, err = hfc.HobbyfarmV1().Scenarios("Hobbyfarm123").Update(context.TODO(), s, v1.UpdateOptions{}) + _, err = hfc.HobbyfarmV1().Scenarios(Namespace).Update(context.TODO(), s, v1.UpdateOptions{}) } else { err = fmt.Errorf("scenario %s already exists and is not managed by hfcli", sGet.GetName()) } @@ -44,11 +44,11 @@ func Apply(s *hf.Scenario, Namespace string, hfc *hfClientSet.Clientset) (err er func Get(name string, Namespace string, hfc *hfClientSet.Clientset) (s *hf.Scenario, err error) { logrus.Infof("downloading scenario %s", name) - return hfc.HobbyfarmV1().Scenarios("Hobbyfarm123").Get(context.TODO(), name, v1.GetOptions{}) + return hfc.HobbyfarmV1().Scenarios(Namespace).Get(context.TODO(), name, v1.GetOptions{}) } func Delete(name string, Namespace string, hfc *hfClientSet.Clientset) (err error) { logrus.Infof("deleting scenario %s", name) - return hfc.HobbyfarmV1().Scenarios("Hobbyfarm123").Delete(context.TODO(), name, v1.DeleteOptions{}) + return hfc.HobbyfarmV1().Scenarios(Namespace).Delete(context.TODO(), name, v1.DeleteOptions{}) }