Skip to content

Commit

Permalink
add node selector (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
glitchcrab authored Aug 15, 2021
1 parent a650a05 commit c3a6bde
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Github workflow to build and test on PRs.
- Flag to provide a nodename to schedule the pod on.

## [0.4.0] - 2021-08-05

Expand Down
12 changes: 10 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
var (
image string
networkPolicy bool
nodeName string
podArgs string
podCommand string
podSecurityPolicy bool
Expand Down Expand Up @@ -94,14 +95,19 @@ time as --podsecuritypolicy to have any effect.
--networkpolicy (default: false)
Apply a NetworkPolicy which allows all ingress and egress traffic.`,
Apply a NetworkPolicy which allows all ingress and egress traffic.
--node-name (default: none)
Attempt to schedule the pod on the named node.`,
Example: `
"sonar create" - accept all defaults. Creates a deployment in namespace
'default' called 'sonar-debug'. The pod image will be 'busybox:latest'
with 'sleep 24h' as the initial command.
"sonar create --image glitchcrab/ubuntu-debug:v1.0 --pod-cmd sleep \
--pod-args 1h" - uses the provided image, command and args.
--pod-args 1h --node-name worker10" - uses the provided image,
command and args, and attempts to schedule the pod on node 'worker10'.
"sonar create --podsecuritypolicy --pod-userid 0 --privileged" - creates
a deployment which runs as root. Also creates a PodSecurityPolicy
Expand All @@ -119,6 +125,7 @@ func init() {

createCmd.Flags().StringVarP(&image, "image", "i", "busybox:latest", "image name (e.g. glitchcrab/ubuntu-debug:latest)")
createCmd.Flags().BoolVar(&networkPolicy, "networkpolicy", false, "create NetworkPolicy (default \"false\")")
createCmd.Flags().StringVarP(&nodeName, "node-name", "", "", "node name to attempt to schedule the pod on")
createCmd.Flags().StringVarP(&podArgs, "pod-args", "a", "24h", "args to pass to pod command")
createCmd.Flags().StringVarP(&podCommand, "pod-command", "c", "sleep", "pod command (aka image entrypoint)")
createCmd.Flags().BoolVar(&podSecurityPolicy, "podsecuritypolicy", false, "create PodSecurityPolicy (default \"false\")")
Expand Down Expand Up @@ -147,6 +154,7 @@ func createSonarDeployment(cmd *cobra.Command, args []string) {
Name: name,
Namespace: namespace,
NetworkPolicy: networkPolicy,
NodeName: nodeName,
PodArgs: podArgs,
PodCommand: podCommand,
PodSecurityPolicy: podSecurityPolicy,
Expand Down
1 change: 1 addition & 0 deletions internal/sonarconfig/sonarconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type SonarConfig struct {
Name string
Namespace string
NetworkPolicy bool
NodeName string
PodArgs string
PodCommand string
PodSecurityPolicy bool
Expand Down
5 changes: 5 additions & 0 deletions service/k8sresource/create_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ func NewDeployment(k8sClientSet *kubernetes.Clientset, ctx context.Context, sona
deployment.Spec.Template.Spec.Containers[0].Args = cmdargs
}

// Add the NodeName if one was provided.
if sonarConfig.NodeName != "" {
deployment.Spec.Template.Spec.NodeName = sonarConfig.NodeName
}

// Create the Deployment
_, err = k8sClientSet.AppsV1().Deployments(sonarConfig.Namespace).Create(ctx, deployment, metav1.CreateOptions{})

Expand Down

0 comments on commit c3a6bde

Please sign in to comment.