1
+ package machine_config
2
+
3
+ import (
4
+ "context"
5
+ "fmt"
6
+ "io/ioutil"
7
+ "path/filepath"
8
+ "time"
9
+
10
+ "github.com/ghodss/yaml"
11
+ g "github.com/onsi/ginkgo/v2"
12
+ o "github.com/onsi/gomega"
13
+ //e2e "k8s.io/kubernetes/test/e2e/framework"
14
+
15
+ mcfgv1 "github.com/openshift/api/machineconfiguration/v1"
16
+ //mcfgv1alpha1 "github.com/openshift/api/machineconfiguration/v1alpha1"
17
+ //ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common"
18
+ corev1 "k8s.io/api/core/v1"
19
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20
+
21
+ mcClient "github.com/openshift/client-go/machineconfiguration/clientset/versioned"
22
+ exutil "github.com/openshift/origin/test/extended/util"
23
+
24
+ "k8s.io/apimachinery/pkg/util/wait"
25
+ )
26
+
27
+ var (
28
+ MCOMachineConfigBaseDir = exutil .FixturePath ("testdata" , "machine_config" )
29
+ oc = exutil .NewCLIWithoutNamespace ("machine-config" )
30
+ )
31
+
32
+ var _ = g .BeforeSuite (func () {
33
+ inputPullSecretName := "my-input-pull"
34
+ ps , err := oc .AsAdmin ().AdminKubeClient ().CoreV1 ().Secrets ("openshift-config" ).Get (context .TODO (), "pull-secret" , metav1.GetOptions {})
35
+ o .Expect (err ).NotTo (o .HaveOccurred (), "Get pull-secret from openshift-config" )
36
+ localInputPullSecret := & corev1.Secret {
37
+ ObjectMeta : metav1.ObjectMeta {
38
+ Name : inputPullSecretName ,
39
+ },
40
+ Data : ps .Data ,
41
+ Type : ps .Type ,
42
+ }
43
+ _ , err = oc .KubeClient ().CoreV1 ().Secrets (mcoNamespace ).Create (context .TODO (), localInputPullSecret , metav1.CreateOptions {})
44
+ o .Expect (err ).NotTo (o .HaveOccurred (), "Create my-input-pull secret in openshift-machine-config" )
45
+
46
+ longLivedTokenName := "long-live-token"
47
+ serviceAccountName := "builder"
48
+ localTokenPullSecret := & corev1.Secret {
49
+ ObjectMeta : metav1.ObjectMeta {
50
+ Name : longLivedTokenName ,
51
+ Namespace : mcoNamespace ,
52
+ Annotations : map [string ]string {
53
+ corev1 .ServiceAccountNameKey : serviceAccountName ,
54
+ },
55
+ },
56
+ Type : corev1 .SecretTypeServiceAccountToken ,
57
+ }
58
+ _ , err = oc .KubeClient ().CoreV1 ().Secrets (mcoNamespace ).Create (context .Background (), localTokenPullSecret , metav1.CreateOptions {})
59
+ o .Expect (err ).NotTo (o .HaveOccurred (), "Create a long-live-token secret in openshift-machine-config" )
60
+ })
61
+
62
+ // This test is [Serial] because it modifies the cluster/machineconfigurations.operator.openshift.io object in each test.
63
+ var _ = g .Describe ("[sig-mco][OCPFeatureGate:OnClusterBuild][Serial] Run tests serially" , func () {
64
+ defer g .GinkgoRecover ()
65
+ var (
66
+ moscFixture = filepath .Join (MCOMachineConfigBaseDir , "machineosconfigurations" , "machineosconfig.yaml" )
67
+ mcpFixture = filepath .Join (MCOMachineConfigBaseDir , "machineconfigpool" , "machineconfigpool.yaml" )
68
+ )
69
+
70
+ g .BeforeEach (func (ctx context.Context ) {
71
+ //skip this test on single node platforms
72
+
73
+ //skipOnSingleNodeTopology(oc)
74
+ })
75
+
76
+ g .AfterEach (func () {
77
+ // Clear out boot image configuration between tests
78
+
79
+ //err := oc.Run("apply").Args("-f", noneMachineSetFixture).Execute()
80
+ //o.Expect(err).NotTo(o.HaveOccurred())
81
+ })
82
+
83
+ g .It ("Should update opted in MCP with the build image from the dockerfile mentioned in MOSC [apigroup:machineconfiguration.openshift.io]" , func () {
84
+ AllNodePoolOptInTest (oc , moscFixture , mcpFixture )
85
+ })
86
+
87
+ })
88
+
89
+ func AllNodePoolOptInTest (oc * exutil.CLI , moscFixture string , mcpFixture string ) {
90
+
91
+ err := oc .Run ("apply" ).Args ("-f" , mcpFixture ).Execute ()
92
+ o .Expect (err ).NotTo (o .HaveOccurred (), "Create MCP Infra" )
93
+
94
+ nodes , err := oc .KubeClient ().CoreV1 ().Nodes ().List (context .TODO (), metav1.ListOptions {})
95
+ o .Expect (err ).NotTo (o .HaveOccurred (), "Get all nodes" )
96
+ for _ , node := range nodes .Items {
97
+ err = oc .AsAdmin ().Run ("label" ).Args ("node" , node .Name , "node-role.kubernetes.io/infra=" + "" ).Execute ()
98
+ o .Expect (err ).NotTo (o .HaveOccurred (), fmt .Sprintf ("Add node %s to MCP infra" , node .Name ))
99
+ }
100
+ err = oc .Run ("apply" ).Args ("-f" , moscFixture ).Execute ()
101
+ o .Expect (err ).NotTo (o .HaveOccurred (), "Create MOSC Infra and opt in Infra pool into OCL" )
102
+
103
+ machineConfigClient , err := mcClient .NewForConfig (oc .KubeFramework ().ClientConfig ())
104
+ o .Expect (err ).NotTo (o .HaveOccurred ())
105
+
106
+ mcp , err := getMCPFromFixture (mcpFixture )
107
+ o .Expect (err ).NotTo (o .HaveOccurred ())
108
+
109
+ waitTime := time .Minute * 20
110
+ ctx , cancel := context .WithTimeout (context .Background (), waitTime )
111
+ defer cancel ()
112
+
113
+ // Wait for MOSB to be created
114
+ err = waitForBuild (ctx , machineConfigClient , mcp )
115
+ o .Expect (err ).NotTo (o .HaveOccurred (), "Waiting for MOSB to be created and builder pod to Succeed" )
116
+
117
+ // Wait for the build Image to be applied to all nodes
118
+ }
119
+
120
+ func getMCPFromFixture (path string ) (* mcfgv1.MachineConfigPool , error ) {
121
+ data , err := ioutil .ReadFile (path )
122
+ if err != nil {
123
+ return nil , err
124
+ }
125
+
126
+ mcp := new (mcfgv1.MachineConfigPool )
127
+ err = yaml .Unmarshal (data , mcp )
128
+ if err != nil {
129
+ return nil , err
130
+ }
131
+
132
+ return mcp , err
133
+ }
134
+
135
+ func waitForBuild (ctx context.Context , clientset * mcClient.Clientset , mcp * mcfgv1.MachineConfigPool ) error {
136
+ return wait .PollUntilContextCancel (ctx , time .Second , true , func (ctx context.Context ) (done bool , err error ) {
137
+
138
+ mosbList , err := clientset .MachineconfigurationV1alpha1 ().MachineOSBuilds ().List (ctx , metav1.ListOptions {})
139
+ if err != nil {
140
+ return false , err
141
+ }
142
+ /*
143
+ isPending := false
144
+ isBuilding := false
145
+ isSuccess := false
146
+ start := time.Now()
147
+ */
148
+ for _ , mosb := range mosbList .Items {
149
+ if mosb .Spec .DesiredConfig .Name == mcp .Spec .Configuration .Name {
150
+ /*
151
+ state := ctrlcommon.NewMachineOSBuildState(mosb)
152
+ if !isPending && state.IsBuildPending() {
153
+ isPending = true
154
+ e2e.Logf("Build %s is now pending after %s", mosb.Name, time.Since(start))
155
+ }
156
+ if !isBuilding && state.IsBuilding() {
157
+ isBuilding = true
158
+ e2e.Logf("Build %s is now running after %s", mosb.Name, time.Since(start))
159
+ }
160
+ if !isSuccess && state.IsBuildSuccess() {
161
+ isSuccess = true
162
+ e2e.Logf("Build %s is complete after %s", mosb.Name, time.Since(start))
163
+ return true, nil
164
+ }
165
+ if state.IsBuildFailure() {
166
+ return false, fmt.Errorf("build %s failed after %s", mosb.Name, time.Since(start))
167
+ }
168
+ */
169
+ }
170
+ }
171
+ return false , nil
172
+ })
173
+ }
0 commit comments