-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor * duplicate deployments * fix: linting
- Loading branch information
1 parent
d38fd73
commit 9b81e3b
Showing
15 changed files
with
485 additions
and
163 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2024 Michele Zanotti <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/telemaco019/duplik8s/pkg/cmd/flags" | ||
"github.com/telemaco019/duplik8s/pkg/utils" | ||
) | ||
|
||
func NewKubeOptions(cmd *cobra.Command, _ []string) (utils.KubeOptions, error) { | ||
var err error | ||
|
||
o := utils.KubeOptions{} | ||
o.Kubeconfig, err = cmd.Flags().GetString(flags.KUBECONFIG) | ||
if err != nil { | ||
return o, err | ||
} | ||
o.Kubecontext, err = cmd.Flags().GetString(flags.KUBECONTEXT) | ||
if err != nil { | ||
return o, err | ||
} | ||
o.Namespace, err = cmd.Flags().GetString(flags.NAMESPACE) | ||
if err != nil { | ||
return o, err | ||
} | ||
|
||
return o, nil | ||
} |
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,44 @@ | ||
/* | ||
* Copyright 2024 Michele Zanotti <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/telemaco019/duplik8s/pkg/core" | ||
"github.com/telemaco019/duplik8s/pkg/deployments" | ||
"github.com/telemaco019/duplik8s/pkg/utils" | ||
) | ||
|
||
func NewDeployCmd(client core.Duplik8sClient) *cobra.Command { | ||
factory := func(opts utils.KubeOptions) (core.Duplik8sClient, error) { | ||
if client == nil { | ||
return deployments.NewClient(opts) | ||
} | ||
return client, nil | ||
} | ||
deployCmd := &cobra.Command{ | ||
Use: "deploy", | ||
Short: "Duplicate a Deployment.", | ||
Args: cobra.MaximumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
run := newDuplicateCmd(factory, "Select a Deployment") | ||
return run(cmd, args) | ||
}, | ||
} | ||
addOverrideFlags(deployCmd) | ||
return deployCmd | ||
} |
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
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,79 @@ | ||
/* | ||
* Copyright 2024 Michele Zanotti <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/telemaco019/duplik8s/pkg/cmd/flags" | ||
"github.com/telemaco019/duplik8s/pkg/core" | ||
"github.com/telemaco019/duplik8s/pkg/utils" | ||
) | ||
|
||
type duplik8sClientFactory func(opts utils.KubeOptions) (core.Duplik8sClient, error) | ||
|
||
func newDuplicateCmd(factory duplik8sClientFactory, selectMessage string) func(cmd *cobra.Command, args []string) error { | ||
return func(cmd *cobra.Command, args []string) error { | ||
opts, err := NewKubeOptions(cmd, args) | ||
if err != nil { | ||
return err | ||
} | ||
client, err := factory(opts) | ||
if err != nil { | ||
return err | ||
} | ||
cmdOverride, err := cmd.Flags().GetStringSlice(flags.COMMAND_OVERRIDE) | ||
if err != nil { | ||
return err | ||
} | ||
argsOverride, err := cmd.Flags().GetStringSlice(flags.ARGS_OVERRIDE) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Avoid printing usage information on errors | ||
cmd.SilenceUsage = true | ||
options := core.PodOverrideOptions{ | ||
Command: cmdOverride, | ||
Args: argsOverride, | ||
} | ||
|
||
var obj core.DuplicableObject | ||
if len(args) == 0 { | ||
obj, err = utils.SelectItem(client, opts.Namespace, selectMessage) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
obj = core.NewPod(args[0], opts.Namespace) | ||
} | ||
|
||
return client.Duplicate(obj, options) | ||
} | ||
} | ||
|
||
func addOverrideFlags(cmd *cobra.Command) { | ||
cmd.Flags().StringSlice( | ||
flags.COMMAND_OVERRIDE, | ||
[]string{"/bin/sh"}, | ||
"Override the command of each container in the Pod.", | ||
) | ||
cmd.Flags().StringSlice( | ||
flags.ARGS_OVERRIDE, | ||
[]string{"-c", "trap 'exit 0' INT TERM KILL; while true; do sleep 1; done"}, | ||
"Override the command of each container in the Pod.", | ||
) | ||
} |
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,22 @@ | ||
/* | ||
* Copyright 2024 Michele Zanotti <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package core | ||
|
||
type Duplik8sClient interface { | ||
List(namespace string) ([]DuplicableObject, error) | ||
Duplicate(obj DuplicableObject, opts PodOverrideOptions) error | ||
} |
Oops, something went wrong.