Skip to content

Commit 8e9a249

Browse files
Add ability to disable oci client init on startup (#243)
* Add option to disable OCI client init on startup
1 parent d1fd50b commit 8e9a249

File tree

5 files changed

+67
-34
lines changed

5 files changed

+67
-34
lines changed

cloud/util/util.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ func InitClientsAndRegion(ctx context.Context, client client.Client, defaultRegi
158158
} else {
159159
clientProvider = defaultClientProvider
160160
}
161+
if clientProvider == nil {
162+
return nil, "", scope.OCIClients{}, errors.New("OCI authentication credentials could not be retrieved from pod or cluster level," +
163+
"please install Cluster API Provider for OCI with OCI authentication credentials or set Cluster Identity in the OCICluster")
164+
}
161165
// Region set at cluster takes highest precedence
162166
if len(clusterAccessor.GetRegion()) > 0 {
163167
clusterRegion = clusterAccessor.GetRegion()

config/manager/manager.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ spec:
3030
- "--feature-gates=MachinePool=${EXP_MACHINE_POOL:=false},OKE=${EXP_OKE:=false}"
3131
- "--metrics-bind-address=127.0.0.1:8080"
3232
- "--logging-format=${LOG_FORMAT:=text}"
33+
- "--init-oci-clients-on-startup=${INIT_OCI_CLIENTS_ON_STARTUP:=true}"
3334
image: controller:latest
3435
name: manager
3536
securityContext:

docs/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- [Provision a PVC on the File Storage Service](./gs/pvc-fss.md)
3030
- [Customize worker nodes](./gs/customize-worker-node.md)
3131
- [Multi Tenancy](./gs/multi-tenancy.md)
32+
- [Advanced Options](./gs/advanced.md)
3233
- [Networking Guide](./networking/networking.md)
3334
- [Default Network Infrastructure](./networking/infrastructure.md)
3435
- [Using Calico](./networking/calico.md)

docs/src/gs/advanced.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Advanced Options
2+
3+
## Disable OCI Client initialization on startup
4+
5+
CAPOCI supports setting OCI principals at [cluster level][cluster-identity], hence CAPOCI can be
6+
installed without providing OCI user credentials. The following environment variable need to be exported
7+
to install CAPOCI without providing any OCI credentials.
8+
9+
```shell
10+
export INIT_OCI_CLIENTS_ON_STARTUP=false
11+
```
12+
13+
If the above setting is used, and [Cluster Identity][cluster-identity] is not used, the OCICluster will
14+
go into error state, and the following error will show up in the CAPOCI pod logs.
15+
16+
`OCI authentication credentials could not be retrieved from pod or cluster level,please install Cluster API Provider for OCI with OCI authentication credentials or set Cluster Identity in the OCICluster`
17+
18+
[cluster-identity]: ./multi-tenancy.md

main.go

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ var (
5858
ociClusterConcurrency int
5959
ociMachineConcurrency int
6060
ociMachinePoolConcurrency int
61+
initOciClientsOnStartup bool
6162
)
6263

6364
const (
@@ -115,6 +116,12 @@ func main() {
115116
5,
116117
"Number of OciMachinePools to process simultaneously",
117118
)
119+
flag.BoolVar(
120+
&initOciClientsOnStartup,
121+
"init-oci-clients-on-startup",
122+
true,
123+
"Initialize OCI clients on startup",
124+
)
118125

119126
opts := zap.Options{
120127
Development: true,
@@ -147,46 +154,48 @@ func main() {
147154
setupLog.Error(err, "unable to start manager")
148155
os.Exit(1)
149156
}
157+
// Setup the context that's going to be used in controllers and for the manager.
158+
ctx := ctrl.SetupSignalHandler()
150159

151-
authConfigDir := os.Getenv(AuthConfigDirectory)
152-
if authConfigDir == "" {
153-
setupLog.Error(err, "auth config directory environment variable is not set")
154-
os.Exit(1)
155-
}
156-
157-
authConfig, err := config.FromDir(authConfigDir)
158-
if err != nil {
159-
setupLog.Error(err, "invalid auth config file")
160-
os.Exit(1)
161-
}
160+
var clientProvider *scope.ClientProvider
161+
var region string
162+
if initOciClientsOnStartup {
163+
authConfigDir := os.Getenv(AuthConfigDirectory)
164+
if authConfigDir == "" {
165+
setupLog.Error(err, "auth config directory environment variable is not set")
166+
os.Exit(1)
167+
}
162168

163-
setupLog.Info("CAPOCI Version", "version", version.GitVersion)
164-
ociAuthConfigProvider, err := config.NewConfigurationProvider(authConfig)
165-
if err != nil {
166-
setupLog.Error(err, "authentication provider could not be initialised")
167-
os.Exit(1)
168-
}
169+
authConfig, err := config.FromDir(authConfigDir)
170+
if err != nil {
171+
setupLog.Error(err, "invalid auth config file")
172+
os.Exit(1)
173+
}
169174

170-
// Setup the context that's going to be used in controllers and for the manager.
171-
ctx := ctrl.SetupSignalHandler()
175+
setupLog.Info("CAPOCI Version", "version", version.GitVersion)
176+
ociAuthConfigProvider, err := config.NewConfigurationProvider(authConfig)
177+
if err != nil {
178+
setupLog.Error(err, "authentication provider could not be initialised")
179+
os.Exit(1)
180+
}
172181

173-
region, err := ociAuthConfigProvider.Region()
174-
if err != nil {
175-
setupLog.Error(err, "unable to get OCI region from AuthConfigProvider")
176-
os.Exit(1)
177-
}
182+
region, err = ociAuthConfigProvider.Region()
183+
if err != nil {
184+
setupLog.Error(err, "unable to get OCI region from AuthConfigProvider")
185+
os.Exit(1)
186+
}
178187

179-
clientProvider, err := scope.NewClientProvider(ociAuthConfigProvider)
180-
if err != nil {
181-
setupLog.Error(err, "unable to create OCI ClientProvider")
182-
os.Exit(1)
183-
}
184-
_, err = clientProvider.GetOrBuildClient(region)
185-
if err != nil {
186-
setupLog.Error(err, "authentication provider could not be initialised")
187-
os.Exit(1)
188+
clientProvider, err = scope.NewClientProvider(ociAuthConfigProvider)
189+
if err != nil {
190+
setupLog.Error(err, "unable to create OCI ClientProvider")
191+
os.Exit(1)
192+
}
193+
_, err = clientProvider.GetOrBuildClient(region)
194+
if err != nil {
195+
setupLog.Error(err, "authentication provider could not be initialised")
196+
os.Exit(1)
197+
}
188198
}
189-
190199
if err = (&controllers.OCIClusterReconciler{
191200
Client: mgr.GetClient(),
192201
Scheme: mgr.GetScheme(),

0 commit comments

Comments
 (0)