From b5b85829df78082a3958cefa448df5bfaf001c98 Mon Sep 17 00:00:00 2001 From: Sam Stoelinga Date: Thu, 21 Jul 2022 14:05:40 -0700 Subject: [PATCH] Fix nits and add copyright / license headers --- cmd/migrate.go | 16 ++++++++++++++++ cmd/mutating.go | 32 ++++++++++++++++++++++++++------ cmd/pspmigrator/main.go | 3 +-- cmd/root.go | 16 ++++++++++++++++ cmd/utils.go | 16 ++++++++++++++++ migrator.go | 16 ++++++++++++++++ migrator_test.go | 16 ++++++++++++++++ pspmutating.go | 16 ++++++++++++++++ pspmutating_test.go | 16 ++++++++++++++++ 9 files changed, 139 insertions(+), 8 deletions(-) diff --git a/cmd/migrate.go b/cmd/migrate.go index 9df64a1..812fc7e 100644 --- a/cmd/migrate.go +++ b/cmd/migrate.go @@ -1,3 +1,19 @@ +/* +Copyright 2022 The Kubernetes Authors. + +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 ( diff --git a/cmd/mutating.go b/cmd/mutating.go index 441a3da..0027260 100644 --- a/cmd/mutating.go +++ b/cmd/mutating.go @@ -1,3 +1,19 @@ +/* +Copyright 2022 The Kubernetes Authors. + +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 ( @@ -7,8 +23,8 @@ import ( "os" "strconv" - "github.com/olekukonko/tablewriter" "github.com/kubernetes-sigs/pspmigrator" + "github.com/olekukonko/tablewriter" "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -24,22 +40,23 @@ func initMutating() { Use: "pod [name of pod]", Short: "Check if a pod is being mutated by a PSP policy", Run: func(cmd *cobra.Command, args []string) { - // Examples for error handling: - // - Use helper functions like e.g. errors.IsNotFound() - // - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message pod := args[0] podObj, err := clientset.CoreV1().Pods(Namespace).Get(context.TODO(), pod, metav1.GetOptions{}) if errors.IsNotFound(err) { fmt.Printf("Pod %s in namespace %s not found\n", pod, Namespace) + os.Exit(1) } else if statusError, isStatus := err.(*errors.StatusError); isStatus { fmt.Printf("Error getting pod %s in namespace %s: %v\n", pod, Namespace, statusError.ErrStatus.Message) + os.Exit(1) } else if err != nil { - panic(err.Error()) + log.Fatalln(err.Error()) + os.Exit(1) } else { mutated, diff, err := pspmigrator.IsPodBeingMutatedByPSP(podObj, clientset) if err != nil { log.Println(err) + os.Exit(1) } if pspName, ok := podObj.ObjectMeta.Annotations["kubernetes.io/psp"]; ok { fmt.Printf("Pod %v is mutated by PSP %v: %v, diff: %v\n", podObj.Name, pspName, mutated, diff) @@ -98,11 +115,14 @@ func initMutating() { pspObj, err := clientset.PolicyV1beta1().PodSecurityPolicies().Get(context.TODO(), pspName, metav1.GetOptions{}) if errors.IsNotFound(err) { fmt.Printf("PodSecurityPolicy %s not found\n", pspName) + os.Exit(1) } else if statusError, isStatus := err.(*errors.StatusError); isStatus { fmt.Printf("Error getting PodSecurityPolicy %s: %v\n", pspName, statusError.ErrStatus.Message) + os.Exit(1) } else if err != nil { - panic(err.Error()) + log.Fatalln(err.Error()) + os.Exit(1) } else { _, fields, annotations := pspmigrator.IsPSPMutating(pspObj) fmt.Printf("PSP profile %v has the following mutating fields: %v and annotations: %v\n", pspName, fields, annotations) diff --git a/cmd/pspmigrator/main.go b/cmd/pspmigrator/main.go index f23a564..5d3df20 100644 --- a/cmd/pspmigrator/main.go +++ b/cmd/pspmigrator/main.go @@ -1,5 +1,5 @@ /* -Copyright 2016 The Kubernetes Authors. +Copyright 2022 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -14,7 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -// Note: the example only works with the code within the same release/branch. package main import ( diff --git a/cmd/root.go b/cmd/root.go index 5d30688..ef20e65 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,3 +1,19 @@ +/* +Copyright 2022 The Kubernetes Authors. + +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 ( diff --git a/cmd/utils.go b/cmd/utils.go index 68e9062..888417c 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -1,3 +1,19 @@ +/* +Copyright 2022 The Kubernetes Authors. + +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 ( diff --git a/migrator.go b/migrator.go index cb8f15d..3a41c49 100644 --- a/migrator.go +++ b/migrator.go @@ -1,3 +1,19 @@ +/* +Copyright 2022 The Kubernetes Authors. + +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 pspmigrator import ( diff --git a/migrator_test.go b/migrator_test.go index 20ea311..eb032f1 100644 --- a/migrator_test.go +++ b/migrator_test.go @@ -1,3 +1,19 @@ +/* +Copyright 2022 The Kubernetes Authors. + +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 pspmigrator import ( diff --git a/pspmutating.go b/pspmutating.go index b90f03d..7b43034 100644 --- a/pspmutating.go +++ b/pspmutating.go @@ -1,3 +1,19 @@ +/* +Copyright 2022 The Kubernetes Authors. + +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 pspmigrator import ( diff --git a/pspmutating_test.go b/pspmutating_test.go index 584e0f8..e6c59eb 100644 --- a/pspmutating_test.go +++ b/pspmutating_test.go @@ -1,3 +1,19 @@ +/* +Copyright 2022 The Kubernetes Authors. + +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 pspmigrator import (