Skip to content

Commit

Permalink
Some cleanup and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cimnine committed May 3, 2021
1 parent 568cc4c commit 8f00524
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 80 deletions.
4 changes: 2 additions & 2 deletions cmd/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func init() {
"Only delete Namespaces after they were empty for this duration, e.g. [1y2mo3w4d5h6m7s]")
}

func validateNsCommandInput(cmd *cobra.Command, args []string) (returnErr error) {
func validateNsCommandInput(cmd *cobra.Command, _ []string) (returnErr error) {
defer showUsageOnError(cmd, returnErr)
if len(config.Resource.Labels) == 0 {
return missingLabelSelectorError(config.Namespace, "namespaces")
Expand All @@ -58,7 +58,7 @@ func validateNsCommandInput(cmd *cobra.Command, args []string) (returnErr error)
return nil
}

func executeNsCleanupCommand(cmd *cobra.Command, args []string) error {
func executeNsCleanupCommand(_ *cobra.Command, _ []string) error {
coreClient, err := kubernetes.NewCoreV1Client()
if err != nil {
return fmt.Errorf("cannot initiate kubernetes client: %w", err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ func (nss NamespacesService) GetEmptyFor(ctx context.Context, namespaces []corev
emptyNamespaces := []corev1.Namespace{}
namespaceMap := make(map[string]struct{}, len(namespaces))

if err := nss.getHelmReleases(ctx, namespaces, namespaceMap); err != nil {
if err := nss.getHelmReleases(namespaceMap); err != nil {
return nil, fmt.Errorf("could not get Helm releases %w", err)
}
if err := nss.getResources(ctx, namespaces, namespaceMap); err != nil {
if err := nss.getResources(ctx, namespaceMap); err != nil {
return nil, fmt.Errorf("could not get Resources %w", err)
}

Expand Down Expand Up @@ -112,7 +112,7 @@ func (nss NamespacesService) GetEmptyFor(ctx context.Context, namespaces []corev
return emptyNamespaces, nil
}

func (nss NamespacesService) getHelmReleases(ctx context.Context, namespaces []corev1.Namespace, namespaceMap map[string]struct{}) error {
func (nss NamespacesService) getHelmReleases(namespaceMap map[string]struct{}) error {
actionConfig := new(action.Configuration)
if err := actionConfig.Init(genericclioptions.NewConfigFlags(true), "", helmDriverSecret, func(format string, v ...interface{}) {
log.Debug(fmt.Sprintf(format, v))
Expand All @@ -135,7 +135,7 @@ func (nss NamespacesService) getHelmReleases(ctx context.Context, namespaces []c
return nil
}

func (nss NamespacesService) getResources(ctx context.Context, namespaces []corev1.Namespace, namespaceMap map[string]struct{}) error {
func (nss NamespacesService) getResources(ctx context.Context, namespaceMap map[string]struct{}) error {
for _, r := range resources {
resourceList, err := nss.dynamicClient.Resource(r).List(ctx, metav1.ListOptions{})
if err != nil {
Expand Down
102 changes: 28 additions & 74 deletions pkg/namespace/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,16 @@ func Test_GetEmptyFor(t *testing.T) {
},
"Delete2Namespaces": {
objs: []runtime.Object{
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ns-1",
Annotations: map[string]string{
cleanAnnotation: time.Now().UTC().Add(-1 * time.Hour).Format(util.TimeFormat),
},
},
},
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ns-2",
Annotations: map[string]string{
cleanAnnotation: time.Now().UTC().Add(-24 * time.Hour).Format(util.TimeFormat),
},
},
},
annotatedNamespace("test-ns-1", time.Now().UTC().Add(-1 * time.Hour).Format(util.TimeFormat)),
annotatedNamespace("test-ns-2", time.Now().UTC().Add(-24 * time.Hour).Format(util.TimeFormat)),
},
deleteAfter: "1s",
want: []string{"test-ns-1", "test-ns-2"},
wantErr: false,
},
"DeleteNoNamespaces": {
objs: []runtime.Object{
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ns-1",
},
},
annotatedNamespace("test-ns-1", ""),
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ns-2",
Expand All @@ -75,78 +57,32 @@ func Test_GetEmptyFor(t *testing.T) {
},
"DeleteNotYetNamespaces": {
objs: []runtime.Object{
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ns-1",
Annotations: map[string]string{
cleanAnnotation: time.Now().UTC().Format(util.TimeFormat),
},
},
},
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ns-2",
Annotations: map[string]string{
cleanAnnotation: time.Now().UTC().Add(2 * time.Hour).Format(util.TimeFormat),
},
},
},
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ns-3",
},
},
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "delete-test-ns",
Annotations: map[string]string{
cleanAnnotation: time.Now().UTC().Add(-23 * time.Hour).Format(util.TimeFormat),
},
},
},
annotatedNamespace("test-ns-1", time.Now().UTC().Format(util.TimeFormat)),
annotatedNamespace("test-ns-2", time.Now().UTC().Add(2 * time.Hour).Format(util.TimeFormat)),
annotatedNamespace("test-ns-3", ""),
annotatedNamespace("delete-test-ns", time.Now().UTC().Add(-23 * time.Hour).Format(util.TimeFormat)),
},
deleteAfter: "24h",
want: []string{},
wantErr: false,
},
"InvalidAnnotationNamespace": {
objs: []runtime.Object{
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "invalid",
Annotations: map[string]string{
cleanAnnotation: "this-is-invalid",
},
},
},
annotatedNamespace("invalid", "this-is-invalid"),
},
want: []string{},
wantErr: true,
},
"NamespaceNotEmpty": {
objs: []runtime.Object{
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "ns1",
Annotations: map[string]string{
cleanAnnotation: time.Now().UTC().Add(-48 * time.Hour).Format(util.TimeFormat),
},
},
},
annotatedNamespace("ns1", time.Now().UTC().Add(-48 * time.Hour).Format(util.TimeFormat)),
&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Namespace: "ns1",
},
},
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "ns2",
Annotations: map[string]string{
cleanAnnotation: time.Now().UTC().Add(-48 * time.Hour).Format(util.TimeFormat),
},
},
},
annotatedNamespace("ns2", time.Now().UTC().Add(-48 * time.Hour).Format(util.TimeFormat)),
&appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "some-deployment",
Expand Down Expand Up @@ -188,3 +124,21 @@ func Test_GetEmptyFor(t *testing.T) {
})
}
}

func annotatedNamespace(name, cleanAnnotationValue string) *corev1.Namespace {
var annotations map[string]string

if cleanAnnotationValue != "" {
annotations = map[string]string{
cleanAnnotation: cleanAnnotationValue,
}
}

return &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Annotations: annotations,
},
}

}

0 comments on commit 8f00524

Please sign in to comment.