/
delete.go
187 lines (168 loc) · 5.54 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 (
"context"
"errors"
"fmt"
"strconv"
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/client"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/spf13/cobra"
k8errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
)
// newCmdDelete --.
func newCmdDelete(rootCmdOptions *RootCmdOptions) (*cobra.Command, *deleteCmdOptions) {
options := deleteCmdOptions{
RootCmdOptions: rootCmdOptions,
}
cmd := cobra.Command{
Use: "delete [integration1] [integration2] ...",
Short: "Delete integrations deployed on Kubernetes",
PreRunE: decode(&options),
RunE: func(cmd *cobra.Command, args []string) error {
if err := options.validate(args); err != nil {
return err
}
return options.run(cmd, args)
},
}
cmd.Flags().Bool("all", false, "Delete all integrations")
return &cmd, &options
}
type deleteCmdOptions struct {
*RootCmdOptions
DeleteAll bool `mapstructure:"all"`
}
func (command *deleteCmdOptions) validate(args []string) error {
if command.DeleteAll && len(args) > 0 {
return errors.New("invalid combination: --all flag is set and at least one integration name is provided")
}
if !command.DeleteAll && len(args) == 0 {
return errors.New("invalid combination: provide one or several integration names or set --all flag for all integrations")
}
return nil
}
func (command *deleteCmdOptions) run(cmd *cobra.Command, args []string) error {
c, err := command.GetCmdClient()
if err != nil {
return err
}
if len(args) != 0 && !command.DeleteAll {
for _, arg := range args {
name := kubernetes.SanitizeName(arg)
integration, err := getIntegration(command.Context, c, name, command.Namespace)
if err != nil {
if k8errors.IsNotFound(err) {
fmt.Fprintln(cmd.OutOrStdout(), "Integration "+name+" not found. Skipped.")
} else {
return err
}
} else {
err := deleteIntegration(command.Context, cmd, c, integration)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), "Integration "+name+" deleted")
}
}
} else if command.DeleteAll {
integrationList := v1.IntegrationList{
TypeMeta: metav1.TypeMeta{
APIVersion: v1.SchemeGroupVersion.String(),
Kind: v1.IntegrationKind,
},
}
err := c.List(command.Context, &integrationList, k8sclient.InNamespace(command.Namespace))
if err != nil {
return err
}
for _, integration := range integrationList.Items {
integration := integration // pin
err := deleteIntegration(command.Context, cmd, c, &integration)
if err != nil {
return err
}
}
if len(integrationList.Items) == 0 {
fmt.Fprintln(cmd.OutOrStdout(), "Nothing to delete")
} else {
fmt.Fprintln(cmd.OutOrStdout(), strconv.Itoa(len(integrationList.Items))+" integration(s) deleted")
}
}
return nil
}
func getIntegration(ctx context.Context, c client.Client, name string, namespace string) (*v1.Integration, error) {
key := k8sclient.ObjectKey{
Name: name,
Namespace: namespace,
}
answer := v1.NewIntegration(namespace, name)
if err := c.Get(ctx, key, &answer); err != nil {
return nil, err
}
return &answer, nil
}
func deleteIntegration(ctx context.Context, cmd *cobra.Command, c client.Client, integration *v1.Integration) error {
deleted, binding, err := deleteKameletBindingIfExists(ctx, c, integration)
if err != nil {
return err
}
if deleted {
// Deleting KameletBinding will automatically clean up the integration
fmt.Fprintln(cmd.OutOrStdout(), "KameletBinding "+binding+" deleted")
return nil
}
return c.Delete(ctx, integration)
}
func deleteKameletBindingIfExists(ctx context.Context, c client.Client, integration *v1.Integration) (bool, string, error) {
kind, name := findCreator(integration)
if kind != v1alpha1.KameletBindingKind || name == "" {
return false, "", nil
}
binding := v1alpha1.KameletBinding{
TypeMeta: metav1.TypeMeta{
Kind: kind,
APIVersion: v1alpha1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: integration.Namespace,
Name: name,
},
}
err := c.Delete(ctx, &binding)
if k8errors.IsNotFound(err) {
// Simply skip if binding doesn't exist (could be deleted already)
return false, name, nil
}
return err == nil, name, err
}
func findCreator(integration *v1.Integration) (string, string) {
kind := integration.GetLabels()[kubernetes.CamelCreatorLabelKind]
name := integration.GetLabels()[kubernetes.CamelCreatorLabelName]
if kind == "" && name == "" {
// Look up in OwnerReferences in case creator labels are absent
for _, owner := range integration.GetOwnerReferences() {
if owner.Kind == v1alpha1.KameletBindingKind {
return owner.Kind, owner.Name
}
}
}
return kind, name
}