-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
097a5c9
commit 61271a1
Showing
57 changed files
with
5,203 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "tortoisectl [COMMANDS]", | ||
Short: "tortoisectl is a CLI for managing Tortoise", | ||
Long: `tortoisectl is a CLI for managing Tortoise.`, | ||
} | ||
|
||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
autoscalingv1beta3 "github.com/mercari/tortoise/api/v1beta3" | ||
"github.com/mercari/tortoise/pkg/deployment" | ||
"github.com/mercari/tortoise/pkg/pod" | ||
"github.com/mercari/tortoise/pkg/stoper" | ||
"github.com/spf13/cobra" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"k8s.io/client-go/tools/record" | ||
"k8s.io/client-go/util/homedir" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
var stopCmd = &cobra.Command{ | ||
Use: "stop tortoise1 tortoise2...", | ||
Short: "stop tortoise(s) safely", | ||
Long: `stop is the command to turn off tortoise(s) safely.`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
// validation | ||
if stopAll { | ||
if len(args) != 0 { | ||
return fmt.Errorf("tortoise name shouldn't be specified because of --all flag") | ||
} | ||
} else { | ||
if stopNamespace == "" { | ||
return fmt.Errorf("namespace must be specified") | ||
} | ||
if len(args) == 0 { | ||
return fmt.Errorf("tortoise name must be specified") | ||
} | ||
} | ||
|
||
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) | ||
if err != nil { | ||
return fmt.Errorf("failed to build config: %v", err) | ||
} | ||
|
||
client, err := client.New(config, client.Options{ | ||
Scheme: scheme, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to create client: %v", err) | ||
} | ||
|
||
recorder := record.NewBroadcaster().NewRecorder(scheme, corev1.EventSource{Component: "tortoisectl"}) | ||
deploymentService := deployment.New(client, "", "", recorder) | ||
podService, err := pod.New(map[string]int64{}, "", nil, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed to create pod service: %v", err) | ||
} | ||
|
||
stoperService := stoper.New(client, deploymentService, podService) | ||
|
||
opts := []stoper.StoprOption{} | ||
if noLoweringResources { | ||
opts = append(opts, stoper.NoLoweringResource) | ||
} | ||
|
||
err = stoperService.Stop(cmd.Context(), args, stopNamespace, stopAll, os.Stdout, opts...) | ||
if err != nil { | ||
return fmt.Errorf("failed to stop tortoise(s): %v", err) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
var ( | ||
// namespace to stop tortoise(s) in | ||
stopNamespace string | ||
// stop all tortoises in the specified namespace, or in all namespaces if no namespace is specified. | ||
stopAll bool | ||
// Stop tortoise without lowering resource requests. | ||
// If this flag is specified and the current Deployment's resource request(s) is lower than the current Pods' request mutated by Tortoise, | ||
// this CLI patches the deployment so that changing tortoise to Off won't result in lowering the resource request(s), damaging the service. | ||
noLoweringResources bool | ||
|
||
// Path to KUBECONFIG | ||
kubeconfig string | ||
|
||
scheme = runtime.NewScheme() | ||
) | ||
|
||
func init() { | ||
utilruntime.Must(clientgoscheme.AddToScheme(scheme)) | ||
utilruntime.Must(autoscalingv1beta3.AddToScheme(scheme)) | ||
|
||
rootCmd.AddCommand(stopCmd) | ||
|
||
if home := homedir.HomeDir(); home != "" { | ||
stopCmd.Flags().StringVar(&kubeconfig, "kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") | ||
} else { | ||
stopCmd.Flags().StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file") | ||
} | ||
|
||
stopCmd.Flags().StringVarP(&stopNamespace, "namespace", "n", "", "namespace to stop tortoise(s) in") | ||
stopCmd.Flags().BoolVarP(&stopAll, "all", "A", false, "stop all tortoises in the specified namespace, or in all namespaces if no namespace is specified.") | ||
stopCmd.Flags().BoolVar(&noLoweringResources, "no-lowering-resources", false, `Stop tortoise without lowering resource requests. | ||
If this flag is specified and the current Deployment's resource request(s) is lower than the current Pods' request mutated by Tortoise, | ||
this CLI patches the deployment so that changing tortoise to Off won't result in lowering the resource request(s), damaging the service.`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
import "github.com/mercari/tortoise/cmd/tortoisectl/commands" | ||
|
||
func main() { | ||
commands.Execute() | ||
} |
48 changes: 48 additions & 0 deletions
48
...ortoisectl/test/testdata/success-all-in-all-namespace/after/deployments/deployment-a.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
metadata: | ||
name: mercari-app-a | ||
namespace: success-all-in-all-namespace | ||
spec: | ||
progressDeadlineSeconds: 600 | ||
replicas: 1 | ||
revisionHistoryLimit: 10 | ||
selector: | ||
matchLabels: | ||
app: mercari | ||
strategy: | ||
rollingUpdate: | ||
maxSurge: 25% | ||
maxUnavailable: 25% | ||
type: RollingUpdate | ||
template: | ||
metadata: | ||
annotations: | ||
kubectl.kubernetes.io/restartedAt: updated | ||
creationTimestamp: null | ||
labels: | ||
app: mercari | ||
spec: | ||
containers: | ||
- image: awesome-mercari-app-image | ||
imagePullPolicy: Always | ||
name: app | ||
resources: | ||
requests: | ||
cpu: "10" | ||
memory: 10Gi | ||
terminationMessagePath: /dev/termination-log | ||
terminationMessagePolicy: File | ||
- image: awesome-istio-proxy-image | ||
imagePullPolicy: Always | ||
name: istio-proxy | ||
resources: | ||
requests: | ||
cpu: "4" | ||
memory: 4Gi | ||
terminationMessagePath: /dev/termination-log | ||
terminationMessagePolicy: File | ||
dnsPolicy: ClusterFirst | ||
restartPolicy: Always | ||
schedulerName: default-scheduler | ||
securityContext: {} | ||
terminationGracePeriodSeconds: 30 | ||
status: {} |
48 changes: 48 additions & 0 deletions
48
...l/test/testdata/success-all-in-all-namespace/after/deployments/deployment-another-ns.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
metadata: | ||
name: mercari-app | ||
namespace: success-all-in-all-namespace-2 | ||
spec: | ||
progressDeadlineSeconds: 600 | ||
replicas: 1 | ||
revisionHistoryLimit: 10 | ||
selector: | ||
matchLabels: | ||
app: mercari | ||
strategy: | ||
rollingUpdate: | ||
maxSurge: 25% | ||
maxUnavailable: 25% | ||
type: RollingUpdate | ||
template: | ||
metadata: | ||
annotations: | ||
kubectl.kubernetes.io/restartedAt: updated | ||
creationTimestamp: null | ||
labels: | ||
app: mercari | ||
spec: | ||
containers: | ||
- image: awesome-mercari-app-image | ||
imagePullPolicy: Always | ||
name: app | ||
resources: | ||
requests: | ||
cpu: "10" | ||
memory: 10Gi | ||
terminationMessagePath: /dev/termination-log | ||
terminationMessagePolicy: File | ||
- image: awesome-istio-proxy-image | ||
imagePullPolicy: Always | ||
name: istio-proxy | ||
resources: | ||
requests: | ||
cpu: "4" | ||
memory: 4Gi | ||
terminationMessagePath: /dev/termination-log | ||
terminationMessagePolicy: File | ||
dnsPolicy: ClusterFirst | ||
restartPolicy: Always | ||
schedulerName: default-scheduler | ||
securityContext: {} | ||
terminationGracePeriodSeconds: 30 | ||
status: {} |
48 changes: 48 additions & 0 deletions
48
...ortoisectl/test/testdata/success-all-in-all-namespace/after/deployments/deployment-b.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
metadata: | ||
name: mercari-app-b | ||
namespace: success-all-in-all-namespace | ||
spec: | ||
progressDeadlineSeconds: 600 | ||
replicas: 1 | ||
revisionHistoryLimit: 10 | ||
selector: | ||
matchLabels: | ||
app: mercari | ||
strategy: | ||
rollingUpdate: | ||
maxSurge: 25% | ||
maxUnavailable: 25% | ||
type: RollingUpdate | ||
template: | ||
metadata: | ||
annotations: | ||
kubectl.kubernetes.io/restartedAt: updated | ||
creationTimestamp: null | ||
labels: | ||
app: mercari | ||
spec: | ||
containers: | ||
- image: awesome-mercari-app-image | ||
imagePullPolicy: Always | ||
name: app | ||
resources: | ||
requests: | ||
cpu: "10" | ||
memory: 10Gi | ||
terminationMessagePath: /dev/termination-log | ||
terminationMessagePolicy: File | ||
- image: awesome-istio-proxy-image | ||
imagePullPolicy: Always | ||
name: istio-proxy | ||
resources: | ||
requests: | ||
cpu: "4" | ||
memory: 4Gi | ||
terminationMessagePath: /dev/termination-log | ||
terminationMessagePolicy: File | ||
dnsPolicy: ClusterFirst | ||
restartPolicy: Always | ||
schedulerName: default-scheduler | ||
securityContext: {} | ||
terminationGracePeriodSeconds: 30 | ||
status: {} |
48 changes: 48 additions & 0 deletions
48
...ortoisectl/test/testdata/success-all-in-all-namespace/after/deployments/deployment-c.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
metadata: | ||
name: mercari-app-c | ||
namespace: success-all-in-all-namespace | ||
spec: | ||
progressDeadlineSeconds: 600 | ||
replicas: 1 | ||
revisionHistoryLimit: 10 | ||
selector: | ||
matchLabels: | ||
app: mercari | ||
strategy: | ||
rollingUpdate: | ||
maxSurge: 25% | ||
maxUnavailable: 25% | ||
type: RollingUpdate | ||
template: | ||
metadata: | ||
annotations: | ||
kubectl.kubernetes.io/restartedAt: updated | ||
creationTimestamp: null | ||
labels: | ||
app: mercari | ||
spec: | ||
containers: | ||
- image: awesome-mercari-app-image | ||
imagePullPolicy: Always | ||
name: app | ||
resources: | ||
requests: | ||
cpu: "10" | ||
memory: 10Gi | ||
terminationMessagePath: /dev/termination-log | ||
terminationMessagePolicy: File | ||
- image: awesome-istio-proxy-image | ||
imagePullPolicy: Always | ||
name: istio-proxy | ||
resources: | ||
requests: | ||
cpu: "4" | ||
memory: 4Gi | ||
terminationMessagePath: /dev/termination-log | ||
terminationMessagePolicy: File | ||
dnsPolicy: ClusterFirst | ||
restartPolicy: Always | ||
schedulerName: default-scheduler | ||
securityContext: {} | ||
terminationGracePeriodSeconds: 30 | ||
status: {} |
Oops, something went wrong.