forked from GoogleCloudPlatform/gcs-fuse-csi-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfake.go
More file actions
109 lines (88 loc) · 2.88 KB
/
fake.go
File metadata and controls
109 lines (88 loc) · 2.88 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
102
103
104
105
106
107
108
109
/*
Copyright 2018 The Kubernetes Authors.
Copyright 2022 Google LLC
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
https://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 clientset
import (
"context"
"github.com/googlecloudplatform/gcs-fuse-csi-driver/pkg/webhook"
authenticationv1 "k8s.io/api/authentication/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type FakeClientset struct {
fakePod *corev1.Pod
fakeNode *corev1.Node
}
func NewFakeClientset() *FakeClientset {
fakeClientSet := &FakeClientset{}
// Default setting for most unit tests is pod doesn't use host network & workload identity is enabled on the node
fakeClientSet.CreatePod( /*hostNetworkEnabled */ false)
fakeClientSet.CreateNode( /* isWorkloadIdentityEnabledOnNode */ true)
return fakeClientSet
}
func (c *FakeClientset) ConfigurePodLister(_ string) {}
func (c *FakeClientset) ConfigureNodeLister(_ string) {}
func (c *FakeClientset) CreatePod(hostNetworkEnabled bool) {
config := webhook.FakeConfig()
c.fakePod = &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "",
Namespace: "",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
webhook.GetSidecarContainerSpec(config),
},
Volumes: webhook.GetSidecarContainerVolumeSpec(),
},
Status: corev1.PodStatus{
ContainerStatuses: []corev1.ContainerStatus{
{
Name: webhook.GcsFuseSidecarName,
State: corev1.ContainerState{
Running: &corev1.ContainerStateRunning{},
},
},
},
},
}
if hostNetworkEnabled {
c.fakePod.Spec.HostNetwork = true
}
}
func (c *FakeClientset) CreateNode(isWorkloadIdentityEnabled bool) {
c.fakeNode = &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "",
Labels: map[string]string{},
},
}
if isWorkloadIdentityEnabled {
c.fakeNode.Labels[GkeMetaDataServerKey] = "true"
}
}
func (c *FakeClientset) GetPod(namespace, name string) (*corev1.Pod, error) {
c.fakePod.ObjectMeta.Name = name
c.fakePod.ObjectMeta.Namespace = namespace
return c.fakePod, nil
}
func (c *FakeClientset) GetNode(name string) (*corev1.Node, error) {
c.fakeNode.ObjectMeta.Name = name
return c.fakeNode, nil
}
func (c *FakeClientset) CreateServiceAccountToken(_ context.Context, _, _ string, _ *authenticationv1.TokenRequest) (*authenticationv1.TokenRequest, error) {
return &authenticationv1.TokenRequest{}, nil
}
func (c *FakeClientset) GetGCPServiceAccountName(_ context.Context, _, _ string) (string, error) {
return "", nil
}