Skip to content

Commit

Permalink
Update kubectl plugins to respect kubeconfig namespace setting & rele…
Browse files Browse the repository at this point in the history
…ase v0.9.0 (#219)

Fixes #193
  • Loading branch information
nstogner committed Aug 25, 2023
1 parent d4f65da commit 3b38da0
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# Image URL to use all building/pushing image targets
VERSION ?= v0.8.3
VERSION ?= v0.9.0
IMG ?= docker.io/substratusai/controller-manager:${VERSION}
IMG_SCI_KIND ?= docker.io/substratusai/sci-kind:${VERSION}
IMG_SCI_GCP ?= docker.io/substratusai/sci-gcp:${VERSION}
Expand Down
2 changes: 1 addition & 1 deletion config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ kind: Kustomization
images:
- name: controller
newName: docker.io/substratusai/controller-manager
newTag: v0.8.3
newTag: v0.9.0
2 changes: 1 addition & 1 deletion config/sci-gcp/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ kind: Kustomization
images:
- name: sci
newName: docker.io/substratusai/sci-gcp
newTag: v0.8.3
newTag: v0.9.0
2 changes: 1 addition & 1 deletion config/sci-kind/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ kind: Kustomization
images:
- name: sci
newName: docker.io/substratusai/sci-kind
newTag: v0.8.3
newTag: v0.9.0
6 changes: 2 additions & 4 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ You can test out the latest kubectl plugin by building from source directly:

```sh
go build ./kubectl/cmd/notebook &&
mv notebook /usr/local/bin/kubectl-notebook ||
sudo mv notebook /usr/local/bin/kubectl-notebook
mv notebook /usr/local/bin/kubectl-notebook
go build ./kubectl/cmd/applybuild &&
mv applybuild /usr/local/bin/kubectl-applybuild ||
sudo mv applybuild /usr/local/bin/kubectl-applybuild
mv applybuild /usr/local/bin/kubectl-applybuild
```

The `kubectl notebook` command depends on container-tools for live-syncing. The plugin will try
Expand Down
4 changes: 2 additions & 2 deletions install/gcp/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1575,7 +1575,7 @@ spec:
envFrom:
- configMapRef:
name: system
image: docker.io/substratusai/controller-manager:v0.8.3
image: docker.io/substratusai/controller-manager:v0.9.0
livenessProbe:
httpGet:
path: /healthz
Expand Down Expand Up @@ -1625,7 +1625,7 @@ spec:
- envFrom:
- configMapRef:
name: system
image: docker.io/substratusai/sci-gcp:v0.8.3
image: docker.io/substratusai/sci-gcp:v0.9.0
livenessProbe:
failureThreshold: 3
initialDelaySeconds: 15
Expand Down
4 changes: 2 additions & 2 deletions install/kind/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1640,7 +1640,7 @@ spec:
envFrom:
- configMapRef:
name: system
image: docker.io/substratusai/controller-manager:v0.8.3
image: docker.io/substratusai/controller-manager:v0.9.0
livenessProbe:
httpGet:
path: /healthz
Expand Down Expand Up @@ -1719,7 +1719,7 @@ spec:
- envFrom:
- configMapRef:
name: system
image: docker.io/substratusai/sci-kind:v0.8.3
image: docker.io/substratusai/sci-kind:v0.9.0
livenessProbe:
failureThreshold: 3
initialDelaySeconds: 15
Expand Down
2 changes: 1 addition & 1 deletion install/kubectl-plugins.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
set -xe

version=v0.8.3
version=v0.9.0
os=$(uname -s)
arch=$(uname -m | sed 's/aarch64/arm64/g' | sed 's/x86_64/amd64/g')

Expand Down
53 changes: 37 additions & 16 deletions kubectl/internal/commands/applybuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ import (
"fmt"
"os"

"k8s.io/klog/v2"

"github.com/spf13/cobra"
"github.com/substratusai/substratus/kubectl/internal/client"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"

"github.com/substratusai/substratus/kubectl/internal/client"
)

func ApplyBuild() *cobra.Command {
var cfg struct {
var flags struct {
namespace string
filename string
build string
kubeconfig string
forceConflicts bool
}

var cmd = &cobra.Command{
cmd := &cobra.Command{
Use: "applybuild [flags] BUILD_CONTEXT",
Args: cobra.ExactArgs(1),
Short: "Apply a Substratus object, upload and build container in-cluster from a local directory",
Expand All @@ -31,28 +32,35 @@ func ApplyBuild() *cobra.Command {

ctx := cmd.Context()

if cfg.filename == "" {
if flags.filename == "" {
return fmt.Errorf("-f (--filename) is required")
}
cfg.build = args[0]
flags.build = args[0]

tarball, err := client.PrepareImageTarball(ctx, cfg.build)
tarball, err := client.PrepareImageTarball(ctx, flags.build)
if err != nil {
return fmt.Errorf("preparing tarball: %w", err)
}
defer os.Remove(tarball.TempDir)

restConfig, err := clientcmd.BuildConfigFromFlags("", cfg.kubeconfig)
kubeconfigNamespace, restConfig, err := buildConfigFromFlags("", flags.kubeconfig)
if err != nil {
return fmt.Errorf("rest config: %w", err)
}

namespace := "default"
if flags.namespace != "" {
namespace = flags.namespace
} else if kubeconfigNamespace != "" {
namespace = kubeconfigNamespace
}

clientset, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return fmt.Errorf("clientset: %w", err)
}

manifest, err := os.ReadFile(cfg.filename)
manifest, err := os.ReadFile(flags.filename)
if err != nil {
return fmt.Errorf("reading file: %w", err)
}
Expand All @@ -61,9 +69,20 @@ func ApplyBuild() *cobra.Command {
if err != nil {
return fmt.Errorf("decoding: %w", err)
}

if obj.GetNamespace() == "" {
// TODO: Add -n flag to specify namespace.
obj.SetNamespace("default")
// When there is no .metadata.namespace set in the manifest...
obj.SetNamespace(namespace)
} else {
// TODO: Closer match kubectl behavior here by differentiaing between
// the short -n and long --namespace flags.
// See example kubectl error:
// error: the namespace from the provided object "a" does not match the namespace "b". You must pass '--namespace=a' to perform this operation.
if flags.namespace != "" && flags.namespace != obj.GetNamespace() {
// When there is .metadata.namespace set in the manifest and
// a conflicting -n or --namespace flag...
return fmt.Errorf("the namespace from the provided object %q does not match the namespace %q from flag", obj.GetNamespace(), flags.namespace)
}
}

c := NewClient(clientset, restConfig)
Expand All @@ -79,7 +98,7 @@ func ApplyBuild() *cobra.Command {
return fmt.Errorf("clearing image: %w", err)
}

if err := r.Apply(obj, cfg.forceConflicts); err != nil {
if err := r.Apply(obj, flags.forceConflicts); err != nil {
return fmt.Errorf("applying: %w", err)
}

Expand All @@ -95,9 +114,11 @@ func ApplyBuild() *cobra.Command {
if defaultKubeconfig == "" {
defaultKubeconfig = clientcmd.RecommendedHomeFile
}
cmd.Flags().StringVarP(&cfg.kubeconfig, "kubeconfig", "", defaultKubeconfig, "")
cmd.Flags().StringVarP(&cfg.filename, "filename", "f", "", "Filename identifying the resource to apply and build.")
cmd.Flags().BoolVar(&cfg.forceConflicts, "force-conflicts", false, "If true, server-side apply will force the changes against conflicts.")
cmd.Flags().StringVarP(&flags.kubeconfig, "kubeconfig", "", defaultKubeconfig, "")
cmd.Flags().StringVarP(&flags.filename, "filename", "f", "", "Filename identifying the resource to apply and build.")
cmd.Flags().BoolVar(&flags.forceConflicts, "force-conflicts", false, "If true, server-side apply will force the changes against conflicts.")

cmd.Flags().StringVarP(&flags.namespace, "namespace", "n", "", "Namespace of Notebook")

// Add standard kubectl logging flags (for example: -v=2).
goflags := flag.NewFlagSet("", flag.PanicOnError)
Expand Down

0 comments on commit 3b38da0

Please sign in to comment.