Skip to content

Commit

Permalink
squashctl refactoring kube client (#153)
Browse files Browse the repository at this point in the history
* Fixed formatting
* getMissing function does not need to be exported
* Refactored kube client code into single package
* Removed unused function
* Fixed type mismatch
* Fixed comment typo
* Consolidated code to Kube Client, and propagated errors better.
* fixed CI script
* More refactoring to late bind other kube context dependent functions
* Removed unused parameters and return values
  • Loading branch information
scranton authored and soloio-bulldozer[bot] committed Mar 27, 2019
1 parent ad900d2 commit 8c3d21c
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 183 deletions.
2 changes: 2 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions changelog/v0.5.6/refactor-kube-client.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changelog:
- type: FIX
description: Consolidated code to get Kubernetes Context client, and added better error handling.
issueLink: https://github.com/solo-io/squash/issues/154
2 changes: 1 addition & 1 deletion ci/check-code-and-docs-gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fi

git init
git add .
git commit -m "set up dummy repo for diffing" -q
git commit -m "set up dummy repo for diffing" -q --allow-empty

git clone https://github.com/solo-io/solo-kit /workspace/gopath/src/github.com/solo-io/solo-kit

Expand Down
4 changes: 2 additions & 2 deletions contrib/templategen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func main() {
}
tmpl := template.Must(template.New("versioned").Parse(string(templatetxt)))
i := Info{
Repo : os.Getenv("SQUASH_REPO"),
Version : os.Getenv("SQUASH_VERSION"),
Repo: os.Getenv("SQUASH_REPO"),
Version: os.Getenv("SQUASH_VERSION"),
}
tmpl.Execute(os.Stdout, i)

Expand Down
9 changes: 6 additions & 3 deletions hack/monitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/solo-io/go-utils/contextutils"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/squash/pkg/api/v1"
v1 "github.com/solo-io/squash/pkg/api/v1"
"github.com/solo-io/squash/pkg/utils"
"github.com/solo-io/squash/pkg/utils/kubeutils"
)
Expand Down Expand Up @@ -45,7 +45,7 @@ func NewMonitor() (Monitor, error) {
}
return Monitor{
ctx: ctx,
daClient: daClient,
daClient: &daClient,
}, nil
}

Expand All @@ -62,7 +62,10 @@ func (m *Monitor) Run() error {
el := v1.NewApiEventLoop(emitter, syncer)
// run event loop
// watch all namespaces
namespaces := kubeutils.MustGetNamespaces(nil)
namespaces, err := kubeutils.MustGetNamespaces(nil)
if err != nil {
return err
}
if customNamespaces == unspecifiedCustomNamespaces {
namespaces = strings.Split(customNamespaces, ",")
}
Expand Down
88 changes: 58 additions & 30 deletions pkg/config/squash.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"strings"
"time"

squashkubeutils "github.com/solo-io/squash/pkg/utils/kubeutils"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

gokubeutils "github.com/solo-io/go-utils/kubeutils"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
squashv1 "github.com/solo-io/squash/pkg/api/v1"
"github.com/solo-io/squash/pkg/debuggers/local"
Expand Down Expand Up @@ -48,9 +49,7 @@ type Squash struct {
}

func NewSquashConfig() Squash {
s := Squash{}
s.clientset = s.getClientSet()
return s
return Squash{}
}

type DebugTarget struct {
Expand All @@ -59,23 +58,29 @@ type DebugTarget struct {
}

func StartDebugContainer(s Squash, dbt DebugTarget) (*v1.Pod, error) {

dbgpod, err := s.debugPodFor()
if err != nil {
return nil, err
}

cs, err := s.getClientSet()
if err != nil {
return nil, err
}

// create namespace. ignore errors as it most likely exists and will error
s.getClientSet().CoreV1().Namespaces().Create(&v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: s.SquashNamespace}})
cs.CoreV1().Namespaces().Create(&v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: s.SquashNamespace}})

// create debugger pod
createdPod, err := s.getClientSet().CoreV1().Pods(s.SquashNamespace).Create(dbgpod)
createdPod, err := cs.CoreV1().Pods(s.SquashNamespace).Create(dbgpod)
if err != nil {
return nil, fmt.Errorf("Could not create pod: %v", err)
}

if !s.Machine && !s.NoClean {
// do not remove the pod on a debug server as it is waiting for a
// connection
// TODO: handle returned error
defer s.deletePod(createdPod)
}

Expand All @@ -96,7 +101,7 @@ func StartDebugContainer(s Squash, dbt DebugTarget) (*v1.Pod, error) {
}

// for the debug controller, this function finds the debug target
// from the squash spec that it recieves
// from the squash spec that it receives
// If it is able to find a unique target, it applies the target
// values to the DebugTarget argument. Otherwise it errors.
func (s *Squash) ExpectToGetUniqueDebugTargetFromSpec(dbt *DebugTarget) error {
Expand All @@ -110,8 +115,11 @@ func (s *Squash) ExpectToGetUniqueDebugTargetFromSpec(dbt *DebugTarget) error {
}

func (s *Squash) GetDebugTargetPodFromSpec(dbt *DebugTarget) error {
var err error
dbt.Pod, err = s.getClientSet().CoreV1().Pods(s.Namespace).Get(s.Pod, meta_v1.GetOptions{})
cs, err := s.getClientSet()
if err != nil {
return err
}
dbt.Pod, err = cs.CoreV1().Pods(s.Namespace).Get(s.Pod, meta_v1.GetOptions{})
if err != nil {
return errors.Wrap(err, "fetching pod")
}
Expand Down Expand Up @@ -237,9 +245,13 @@ func (s *Squash) getDebugAttachment() (*squashv1.DebugAttachment, error) {
return intent.GetDebugAttachment(daClient)
}

func (s *Squash) deletePod(createdPod *v1.Pod) {
func (s *Squash) deletePod(createdPod *v1.Pod) error {
var options meta_v1.DeleteOptions
s.getClientSet().CoreV1().Pods(s.Namespace).Delete(createdPod.ObjectMeta.Name, &options)
cs, err := s.getClientSet()
if err != nil {
return err
}
return cs.CoreV1().Pods(s.Namespace).Delete(createdPod.ObjectMeta.Name, &options)
}

func (s *Squash) waitForPod(ctx context.Context, createdPod *v1.Pod) <-chan error {
Expand All @@ -257,8 +269,13 @@ func (s *Squash) waitForPod(ctx context.Context, createdPod *v1.Pod) <-chan erro

var options meta_v1.GetOptions
options.ResourceVersion = createdPod.ResourceVersion
var err error
createdPod, err = s.getClientSet().CoreV1().Pods(s.SquashNamespace).Get(name, options)

cs, err := s.getClientSet()
if err != nil {
errchan <- err
return
}
createdPod, err = cs.CoreV1().Pods(s.SquashNamespace).Get(name, options)

if createdPod.Status.Phase == v1.PodPending {
if !s.Machine {
Expand Down Expand Up @@ -293,8 +310,14 @@ func (s *Squash) waitForPod(ctx context.Context, createdPod *v1.Pod) <-chan erro
}

func (s *Squash) printError(podName string) error {
cs, err := s.getClientSet()
if err != nil {
return err
}

var options v1.PodLogOptions
req := s.getClientSet().Core().Pods(s.Namespace).GetLogs(podName, &options)

req := cs.CoreV1().Pods(s.Namespace).GetLogs(podName, &options)

readCloser, err := req.Stream()
if err != nil {
Expand Down Expand Up @@ -323,7 +346,11 @@ func containerNameFromSpec(debugger string) string {
func (s *Squash) debugPodFor() (*v1.Pod, error) {
it := s.GetIntent()
const crisockvolume = "crisock"
targetPod, err := s.getClientSet().CoreV1().Pods(it.Pod.Namespace).Get(it.Pod.Name, meta_v1.GetOptions{})
cs, err := s.getClientSet()
if err != nil {
return nil, err
}
targetPod, err := cs.CoreV1().Pods(it.Pod.Namespace).Get(it.Pod.Name, meta_v1.GetOptions{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -395,21 +422,16 @@ func (s *Squash) debugPodFor() (*v1.Pod, error) {
return templatePod, nil
}

func (s *Squash) getClientSet() kubernetes.Interface {
if s.clientset != nil {
return s.clientset
}
restCfg, err := gokubeutils.GetConfig("", "")
if err != nil {
panic(err)
}
cs, err := kubernetes.NewForConfig(restCfg)
if err != nil {
panic(err)
func (s *Squash) getClientSet() (kubernetes.Interface, error) {
if s.clientset == nil {
cs, err := squashkubeutils.GetKubeClient()
if err != nil {
return nil, err
}
s.clientset = cs
}
s.clientset = cs
return s.clientset

return s.clientset, nil
}

// DeletePlankPod deletes the plank pod that was created for the debug session
Expand All @@ -421,10 +443,16 @@ func (s *Squash) DeletePlankPod() error {
if err != nil {
return err
}

da, err := intent.GetDebugAttachment(daClient)
if err != nil {
return err
}

return s.clientset.CoreV1().Pods(s.SquashNamespace).Delete(da.PlankName, &meta_v1.DeleteOptions{})
cs, err := s.getClientSet()
if err != nil {
return err
}

return cs.CoreV1().Pods(s.SquashNamespace).Delete(da.PlankName, &meta_v1.DeleteOptions{})
}
Loading

0 comments on commit 8c3d21c

Please sign in to comment.