/
resource.go
74 lines (60 loc) · 1.73 KB
/
resource.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
package k8s
import (
"context"
"errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"github.com/seal-io/walrus/pkg/dao/model"
"github.com/seal-io/walrus/pkg/dao/types"
"github.com/seal-io/walrus/pkg/operator/k8s/intercept"
"github.com/seal-io/walrus/pkg/operator/k8s/kube"
)
// resourceParsingError emits if the given model.ResourceComponent got deployer error.
type resourceParsingError string
func (e resourceParsingError) Error() string {
return "error resource parsing: " + string(e)
}
// isResourceParsingError returns true if the given error is resourceParsingError.
func isResourceParsingError(err error) bool {
var e resourceParsingError
return errors.As(err, &e)
}
// resource holds the GVR, namespace and name of a Kubernetes resource.
type resource struct {
schema.GroupVersionResource
Namespace string
Name string
}
// parseOperableResources parse the given model.ResourceComponent,
// and keeps resource item which matches enforcer validation.
func parseResources(
ctx context.Context,
op Operator,
res *model.ResourceComponent,
enforcer intercept.Enforcer,
) ([]resource, error) {
if res.DeployerType != types.DeployerTypeTF {
return nil, resourceParsingError("unknown deployer type: " + res.DeployerType)
}
switch res.Type {
case "helm_release":
return parseResourcesOfHelm(ctx, op, res, enforcer.AllowGVK)
case "kubectl_manifest":
return parseResourcesOfKubectlManifest(res, enforcer.AllowGVR)
}
gvr, ok := intercept.Terraform().GetGVR(res.Type)
if !ok {
return nil, nil
}
if !enforcer.AllowGVR(gvr) {
return nil, nil
}
ns, n := kube.ParseNamespacedName(res.Name)
rs := []resource{
{
GroupVersionResource: gvr,
Namespace: ns,
Name: n,
},
}
return rs, nil
}