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

Add kubectl mcing restore MINECRAFT_NAME #52

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,22 @@ test: test-tools
.PHONY: release-build
release-build: kustomize
mkdir -p build
$(MAKE) kubectl-mcing GOOS=windows GOARCH=amd64 SUFFIX=.exe
$(MAKE) kubectl-mcing GOOS=darwin GOARCH=amd64
$(MAKE) kubectl-mcing GOOS=darwin GOARCH=arm64
$(MAKE) kubectl-mcing GOOS=linux GOARCH=amd64
$(MAKE) kubectl-mcing GOOS=linux GOARCH=arm64
$(KUSTOMIZE) build . > build/install.yaml
$(KUSTOMIZE) build config/samples > build/minecraft-sample.yaml

.PHONY: kubectl-mcing
kubectl-mcing: build/kubectl-moco-$(GOOS)-$(GOARCH)$(SUFFIX)

build: $(BUILD_FILES)
mkdir -p $(BIN_DIR)
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags "$(GO_LDFLAGS)" -a -o bin/mcing-controller cmd/mcing-controller/main.go
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags "$(GO_LDFLAGS)" -a -o bin/mcing-init cmd/mcing-init/main.go
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags "$(GO_LDFLAGS)" -a -o bin/kubectl-mcing cmd/kubectl-mcing/main.go

build-image:
docker build --target controller -t mcing-controller:dev .
Expand Down
9 changes: 9 additions & 0 deletions cmd/kubectl-mcing/cmd/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cmd

import (
mcingv1alpha1 "github.com/kmdkuk/mcing/api/v1alpha1"
)

func getPodName(mc *mcingv1alpha1.Minecraft) string {
return mc.PrefixedName() + "-0"
}
68 changes: 68 additions & 0 deletions cmd/kubectl-mcing/cmd/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cmd

import (
"context"
"os"
"time"

mcingv1alpha1 "github.com/kmdkuk/mcing/api/v1alpha1"
"github.com/kmdkuk/mcing/pkg/constants"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdexec "k8s.io/kubectl/pkg/cmd/exec"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)

var execConfig struct {
stdin bool
tty bool
}

var execCmd = &cobra.Command{
Use: "exec MINECRAFT_NAME --- [COMMANDS]",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runRconCLI(cmd.Context(), args[0], cmd, args[1:])
},
}

func runRconCLI(ctx context.Context, mcName string, cmd *cobra.Command, args []string) error {
mc := &mcingv1alpha1.Minecraft{}
err := kubeClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: mcName}, mc)
if err != nil {
return err
}

podName := getPodName(mc)
commands := append([]string{podName, "--", "rcon-cli"}, args...)
argsLenAtDash := 2
options := &cmdexec.ExecOptions{
StreamOptions: cmdexec.StreamOptions{
IOStreams: genericclioptions.IOStreams{
In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stdout,
},
Stdin: execConfig.stdin,
TTY: execConfig.tty,
ContainerName: constants.MinecraftContainerName,
},

Executor: &cmdexec.DefaultRemoteExecutor{},
}
cmdutil.AddPodRunningTimeoutFlag(cmd, 3*time.Minute)
cmdutil.CheckErr(options.Complete(factory, cmd, commands, argsLenAtDash))
cmdutil.CheckErr(options.Validate())
cmdutil.CheckErr(options.Run())

return nil
}

func init() {
fs := execCmd.Flags()
fs.BoolVarP(&execConfig.stdin, "stdin", "i", false, "Pass stdin to the mysql container")
fs.BoolVarP(&execConfig.tty, "tty", "t", false, "Allocate a TTY to stdin")

rootCmd.AddCommand(execCmd)
}
76 changes: 76 additions & 0 deletions cmd/kubectl-mcing/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package cmd

import (
"flag"
"fmt"
"os"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
"k8s.io/apimachinery/pkg/runtime"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/klog"

mcingv1alpha1 "github.com/kmdkuk/mcing/api/v1alpha1"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericclioptions"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var (
kubeConfigFlags *genericclioptions.ConfigFlags
kubeClient client.Client
factory util.Factory
namespace string
)

func init() {
klog.InitFlags(nil)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
kubeConfigFlags = genericclioptions.NewConfigFlags(true)
kubeConfigFlags.AddFlags(rootCmd.PersistentFlags())
}

var rootCmd = &cobra.Command{
Use: "kubectl-mcing",
Short: "kubectl mcing",
Long: "kubectl mcing",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

factory = util.NewFactory(util.NewMatchVersionFlags(kubeConfigFlags))
restConfig, err := factory.ToRESTConfig()
if err != nil {
return err
}

scheme := runtime.NewScheme()
err = clientgoscheme.AddToScheme(scheme)
if err != nil {
return err
}

err = mcingv1alpha1.AddToScheme(scheme)
if err != nil {
return err
}

kubeClient, err = client.New(restConfig, client.Options{Scheme: scheme})
if err != nil {
return err
}

namespace, _, err = kubeConfigFlags.ToRawKubeConfigLoader().Namespace()
return err
},
}

func Execute() {
defer klog.Flush()
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
7 changes: 7 additions & 0 deletions cmd/kubectl-mcing/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/kmdkuk/mcing/cmd/kubectl-mcing/cmd"

func main() {
cmd.Execute()
}
7 changes: 6 additions & 1 deletion e2e/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ BIN_DIR := ${PROJECT_DIR}/bin
KIND := ${BIN_DIR}/kind
KUSTOMIZE := ${BIN_DIR}/kustomize
KUBECTL := ${BIN_DIR}/kubectl
KUBECTL_MCING := ${BIN_DIR}/kubectl-mcing
KUBECONFIG := $(shell pwd)/.kubeconfig
KIND_CONFIG = kind-config.yaml
export KUBECTL KUBECONFIG
Expand All @@ -18,7 +19,7 @@ export KUBECTL KUBECONFIG
setup: $(KIND) $(KUBECTL)

.PHONY: start
start: $(KIND) $(KUBECTL)
start: $(KIND) $(KUBECTL) $(KUBECTL_MCING)
$(KIND) create cluster --name=mcing --config=$(KIND_CONFIG) --image=kindest/node:v$(KUBERNETES_VERSION) --wait 1m
$(MAKE) load
$(MAKE) cert-manager
Expand Down Expand Up @@ -74,3 +75,7 @@ $(KUSTOMIZE):
mkdir -p $(BIN_DIR)
curl -fsL https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv$(KUSTOMIZE_VERSION)/kustomize_v$(KUSTOMIZE_VERSION)_linux_amd64.tar.gz | \
tar -C $(BIN_DIR) -xzf -

$(KUBECTL_MCING):
mkdir -p ../bin
cd ..; GOBIN=$$(pwd)/bin go install ./cmd/kubectl-mcing
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ require (
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.16.0
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
go.uber.org/zap v1.19.1
google.golang.org/grpc v1.42.0
k8s.io/api v0.22.3
k8s.io/apimachinery v0.22.3
k8s.io/cli-runtime v0.22.3
k8s.io/client-go v0.22.3
k8s.io/klog v1.0.0
k8s.io/kubectl v0.22.3
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b
sigs.k8s.io/controller-runtime v0.10.2
)
Loading