Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update hfClient to v1 to include Namespaces #4

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion pkg/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion pkg/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
5 changes: 3 additions & 2 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -15,7 +16,7 @@ type Hfcli struct {

var (
Namespace string
HfClient *hfClientSet.HobbyfarmV1Client
HfClient *hfClientSet.Clientset
)

func App() *cobra.Command {
Expand Down
46 changes: 25 additions & 21 deletions pkg/info/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,41 @@ 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
}

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) {
Expand Down Expand Up @@ -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
}
Expand All @@ -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")
Expand All @@ -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) {
Expand All @@ -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")
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
18 changes: 9 additions & 9 deletions pkg/scenario/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(Namespace).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(Namespace).Create(context.TODO(), s, v1.CreateOptions{})
return err
} else {
return err
Expand All @@ -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(Namespace).Update(context.TODO(), s, v1.UpdateOptions{})
} else {
err = fmt.Errorf("scenario %s already exists and is not managed by hfcli", sGet.GetName())
}
Expand All @@ -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(Namespace).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(Namespace).Delete(context.TODO(), name, v1.DeleteOptions{})
}