-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathconnection.go
More file actions
101 lines (84 loc) · 2.96 KB
/
connection.go
File metadata and controls
101 lines (84 loc) · 2.96 KB
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
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1
package shared
import (
"fmt"
"strings"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/v11/providers/k8s/connection/shared/resources"
admissionv1 "k8s.io/api/admission/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/version"
)
const (
OPTION_GIT_HTTP = "http-url"
OPTION_MANIFEST = "path"
OPTION_IMMEMORY_CONTENT = "manifest-content"
OPTION_NAMESPACE = "namespaces"
OPTION_NAMESPACE_EXCLUDE = "namespaces-exclude"
OPTION_ADMISSION = "k8s-admission-review"
OPTION_OBJECT_KIND = "object-kind"
OPTION_CONTEXT = "context"
OPTION_KUBELOGIN = "kubelogin"
IdPrefix = "//platformid.api.mondoo.app/runtime/k8s/uid/"
)
type ConnectionType string
type Connection interface {
plugin.Connection
Name() string
Runtime() string
// Resources returns the resources that match the provided kind and name. If not kind and name
// are provided, then all cluster resources are returned.
Resources(kind string, name string, namespace string) (*ResourceResult, error)
ServerVersion() *version.Info
SupportedResourceTypes() (*resources.ApiResourceIndex, error)
Platform() *inventory.Platform
Asset() *inventory.Asset
AssetId() (string, error)
BasePlatformId() (string, error)
AdmissionReviews() ([]admissionv1.AdmissionReview, error)
Namespace(name string) (*v1.Namespace, error)
Namespaces() ([]v1.Namespace, error)
InventoryConfig() *inventory.Config
}
type ClusterInfo struct {
Name string
}
type ResourceResult struct {
Name string
Kind string
ResourceType *resources.ApiResource // resource type that matched kind
// Resources the resources that match the name, kind and namespace
Resources []runtime.Object
Namespace string
AllNs bool
}
func sliceToPtrSlice[T any](items []T) []*T {
ptrItems := make([]*T, 0, len(items))
for i := range items {
ptrItems = append(ptrItems, &items[i])
}
return ptrItems
}
func NewPlatformId(assetId string) string {
return IdPrefix + assetId
}
func NewWorkloadPlatformId(basePlatformId, clusterIdentifier, workloadType, namespace, name, uid string) string {
if workloadType == "namespace" {
return NewNamespacePlatformId(basePlatformId, name, uid)
}
platformIdentifier := clusterIdentifier
// when mondoo is called with "--namespace xyz" the cluster identifier already contains the namespace
// when called without the namespace, it is missing, but we need it to identify workloads
if !strings.Contains(clusterIdentifier, "namespace") && namespace != "" {
platformIdentifier += "/namespace/" + namespace
}
// add plural "s"
platformIdentifier += "/" + workloadType + "s" + "/name/" + name
return platformIdentifier
}
func NewNamespacePlatformId(basePlatformId, name, uid string) string {
return fmt.Sprintf("%s%s/namespace/%s", basePlatformId, uid, name)
}