-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement tortoisectl stop command (#400)
* Implement tortoisectl stop command * add documentation * fix lint
- Loading branch information
1 parent
ab706a7
commit dfc57b0
Showing
59 changed files
with
5,232 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,121 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"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" | ||
|
||
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" | ||
) | ||
|
||
var stopCmd = &cobra.Command{ | ||
Use: "stop tortoise1 tortoise2...", | ||
Short: "stop tortoise(s) safely", | ||
Long: ` | ||
stop is the command to temporarily turn off tortoise(s) easily and safely. | ||
It's intended to be used when your application is facing issues that might be caused by tortoise. | ||
Specifically, it changes the tortoise updateMode to "Off" and restarts the deployment to bring the pods back to the original resource requests. | ||
Also, with the --no-lowering-resources flag, it patches the deployment directly | ||
so that changing tortoise to Off won't result in lowering the resource request(s), damaging the service. | ||
e.g., if the Deployment declares 1 CPU request, and the current Pods' request is 2 CPU mutated by Tortoise, | ||
it'd patch the deployment to 2 CPU request to prevent a possible negative impact on the service. | ||
`, | ||
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.