Skip to content

Commit f648ea3

Browse files
authored
Merge pull request #299 from oracle/config_restructure_upstream
Flexvolume Improvements
2 parents bdf664c + 929289a commit f648ea3

File tree

15 files changed

+318
-582
lines changed

15 files changed

+318
-582
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ FROM oraclelinux:7-slim
1717
COPY dist/oci-cloud-controller-manager /usr/local/bin/
1818
COPY dist/oci-flexvolume-driver /usr/local/bin/
1919
COPY dist/oci-volume-provisioner /usr/local/bin/
20-
COPY image/install.sh /usr/local/bin/install.sh
20+
COPY image/* /usr/local/bin/

image/install.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2017 Oracle and/or its affiliates. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import argparse
18+
from string import Template
19+
import subprocess
20+
import os.path
21+
import os
22+
from shutil import copyfile
23+
import base64
24+
import select
25+
26+
parser = argparse.ArgumentParser()
27+
parser.add_argument("-c", "--cloud-config", dest="config", default="/etc/oci/cloud-provider.yaml")
28+
parser.add_argument("-d", "--driver-mount", dest="driver_mount", default="/flexmnt")
29+
30+
options = parser.parse_args()
31+
32+
VENDOR = "oracle"
33+
DRIVER = "oci"
34+
DRIVER_EXEC_PATH= "/usr/local/bin/oci-flexvolume-driver"
35+
36+
DRIVER_DIRECTORY = "{}/{}~{}".format(options.driver_mount, VENDOR, DRIVER)
37+
38+
LOG_FILE = "{}/oci_flexvolume_driver.log".format(DRIVER_DIRECTORY)
39+
40+
41+
def create_driver_directory():
42+
if not os.path.isdir(DRIVER_DIRECTORY):
43+
os.mkdir(DRIVER_DIRECTORY)
44+
45+
def copy_driver_binary():
46+
#Copy executable atomically
47+
copyfile(DRIVER_EXEC_PATH, "{}/.{}".format(DRIVER_DIRECTORY, DRIVER))
48+
os.rename("{}/.{}".format(DRIVER_DIRECTORY, DRIVER), "{}/{}".format(DRIVER_DIRECTORY, DRIVER))
49+
os.chmod("{}/{}".format(DRIVER_DIRECTORY, DRIVER), 0755)
50+
51+
def generate_kubeconfig():
52+
script_path = os.path.abspath(os.path.dirname(__file__))
53+
template_path = os.path.join(script_path, "kubeconfig.yml.template")
54+
with open(template_path, "r") as template_file, open("/var/run/secrets/kubernetes.io/serviceaccount/ca.crt", "r") as ca_file, open("/var/run/secrets/kubernetes.io/serviceaccount/token", "r") as token_file:
55+
template = Template(template_file.read())
56+
result = template.substitute({
57+
"ca" : base64.b64encode(ca_file.read()),
58+
"token" : token_file.read(),
59+
"server" : "https://{}:{}".format(os.getenv("KUBERNETES_SERVICE_HOST", "0.0.0.0"), os.getenv("KUBERNETES_SERVICE_PORT", "443"))
60+
})
61+
with open("{}/kubeconfig".format(DRIVER_DIRECTORY),"w+") as kubeconfig:
62+
kubeconfig.write(result)
63+
64+
def create_log():
65+
with open(LOG_FILE, "w+") as log:
66+
log.write("---OCI FLEXVOLUME DRIVER---\n")
67+
68+
def tail_log():
69+
log_process = subprocess.Popen(['tail', '-F', LOG_FILE], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
70+
while True:
71+
print log_process.stdout.readline()
72+
73+
def copy_config_to_driver_dir():
74+
if os.path.isfile(options.config):
75+
copyfile(options.config, "{}/config.yaml".format(DRIVER_DIRECTORY))
76+
else:
77+
with open(LOG_FILE, "w+") as log:
78+
log.write("Could not copy configuration from {}. Assuming worker node\n".format(options.config))
79+
80+
create_driver_directory()
81+
copy_driver_binary()
82+
create_log()
83+
copy_config_to_driver_dir()
84+
generate_kubeconfig()
85+
tail_log()

image/install.sh

Lines changed: 0 additions & 54 deletions
This file was deleted.

image/kubeconfig.yml.template

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: v1
2+
kind: Config
3+
clusters:
4+
- name: default-cluster
5+
cluster:
6+
certificate-authority-data: $ca
7+
server: $server
8+
contexts:
9+
- name: default-context
10+
context:
11+
cluster: default-cluster
12+
namespace: default
13+
user: default-user
14+
current-context: default-context
15+
users:
16+
- name: default-user
17+
user:
18+
token: $token

manifests/flexvolume-driver/oci-flexvolume-driver.yaml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,10 @@ spec:
3535
- name: config
3636
secret:
3737
secretName: oci-flexvolume-driver
38-
- name: kubeconfig
39-
secret:
40-
secretName: oci-flexvolume-driver-kubeconfig
4138
containers:
4239
- name: oci-flexvolume-driver
4340
image: iad.ocir.io/oracle/cloud-provider-oci:latest
44-
command: ["/bin/bash", "/usr/local/bin/install.sh"]
41+
command: ["/usr/local/bin/install.py", "-c", "/tmp/config.yaml"]
4542
securityContext:
4643
privileged: true
4744
volumeMounts:
@@ -50,9 +47,6 @@ spec:
5047
- mountPath: /tmp
5148
name: config
5249
readOnly: true
53-
- mountPath: /tmp2
54-
name: kubeconfig
55-
readOnly: true
5650
---
5751
apiVersion: apps/v1
5852
kind: DaemonSet
@@ -80,7 +74,7 @@ spec:
8074
containers:
8175
- name: oci-flexvolume-driver
8276
image: iad.ocir.io/oracle/cloud-provider-oci:latest
83-
command: ["/bin/bash", "/usr/local/bin/install.sh"]
77+
command: ["/usr/local/bin/install.py"]
8478
securityContext:
8579
privileged: true
8680
volumeMounts:

pkg/cloudprovider/providers/oci/config/config.go

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package config
1616

1717
import (
18+
"github.com/oracle/oci-cloud-controller-manager/pkg/oci/instance/metadata"
1819
"io"
1920
"os"
2021

@@ -43,14 +44,16 @@ type AuthConfig struct {
4344
// The fields below are deprecated and remain purely for backwards compatibility.
4445
// At some point these need to be removed.
4546

46-
// When set to true, clients will use an instance principal configuration provider
47-
// and ignore auth fields.
47+
// UseInstancePrincipals is DEPRECATED should use top-level UseInstancePrincipals
4848
UseInstancePrincipals bool `yaml:"useInstancePrincipals"`
4949
// CompartmentID is DEPRECATED and should be set on the top level Config
5050
// struct.
5151
CompartmentID string `yaml:"compartment"`
5252
// PrivateKeyPassphrase is DEPRECATED in favour of Passphrase.
5353
PrivateKeyPassphrase string `yaml:"key_passphrase"`
54+
55+
//Metadata service to help fill in certain fields
56+
metadataSvc metadata.Interface
5457
}
5558

5659
const (
@@ -99,13 +102,15 @@ type RateLimiterConfig struct {
99102
RateLimitBucketWrite int `yaml:"rateLimitBucketWrite"`
100103
}
101104

102-
// Config holds the OCI cloud-provider config passed to Kubernetes compontents
105+
// Config holds the OCI cloud-provider config passed to Kubernetes components
103106
// via the --cloud-config option.
104107
type Config struct {
105108
Auth AuthConfig `yaml:"auth"`
106109
LoadBalancer *LoadBalancerConfig `yaml:"loadBalancer"`
107110
RateLimiter *RateLimiterConfig `yaml:"rateLimiter"`
108111

112+
RegionKey string `yaml:"regionKey"`
113+
109114
// When set to true, clients will use an instance principal configuration provider and ignore auth fields.
110115
UseInstancePrincipals bool `yaml:"useInstancePrincipals"`
111116
// CompartmentID is the OCID of the Compartment within which the cluster
@@ -114,32 +119,76 @@ type Config struct {
114119
// VCNID is the OCID of the Virtual Cloud Network (VCN) within which the
115120
// cluster resides.
116121
VCNID string `yaml:"vcn"`
122+
123+
//Metadata service to help fill in certain fields
124+
metadataSvc metadata.Interface
117125
}
118126

119-
// Complete the config applying defaults / overrides.
120-
func (c *Config) Complete() {
121-
if c.LoadBalancer != nil && !c.LoadBalancer.Disabled && c.LoadBalancer.SecurityListManagementMode == "" {
122-
c.LoadBalancer.SecurityListManagementMode = ManagementModeAll // default
123-
if c.LoadBalancer.DisableSecurityListManagement {
127+
// Complete the load balancer config applying defaults / overrides.
128+
func (c *LoadBalancerConfig) Complete() {
129+
if c.Disabled {
130+
return
131+
}
132+
if len(c.SecurityListManagementMode) == 0 {
133+
if c.DisableSecurityListManagement {
124134
zap.S().Warnf("cloud-provider config: \"loadBalancer.disableSecurityListManagement\" is DEPRECATED and will be removed in a later release. Please set \"loadBalancer.SecurityListManagementMode: %s\".", ManagementModeNone)
125-
c.LoadBalancer.SecurityListManagementMode = ManagementModeNone
135+
c.SecurityListManagementMode = ManagementModeNone
136+
} else {
137+
c.SecurityListManagementMode = ManagementModeAll
138+
}
139+
}
140+
}
141+
142+
// Complete the authentication config applying defaults / overrides.
143+
func (c *AuthConfig) Complete() {
144+
if len(c.Passphrase) == 0 && len(c.PrivateKeyPassphrase) > 0 {
145+
zap.S().Warn("cloud-provider config: auth.key_passphrase is DEPRECIATED and will be removed in a later release. Please set auth.passphrase instead.")
146+
c.Passphrase = c.PrivateKeyPassphrase
147+
}
148+
if c.Region == "" || c.CompartmentID == "" {
149+
meta, err := c.metadataSvc.Get()
150+
if err != nil {
151+
zap.S().Warn("cloud-provider config: Unable to access metadata on instance. Will not be able to complete configuration if items are missing")
152+
return
153+
}
154+
if c.Region == "" {
155+
c.Region = meta.CanonicalRegionName
156+
}
157+
158+
if c.CompartmentID == "" {
159+
c.CompartmentID = meta.CompartmentID
126160
}
127161
}
162+
}
128163

164+
// Complete the top-level config applying defaults / overrides.
165+
func (c *Config) Complete() {
166+
if c.LoadBalancer != nil {
167+
c.LoadBalancer.Complete()
168+
}
169+
c.Auth.Complete()
129170
// Ensure backwards compatibility fields are set correctly.
130-
if c.CompartmentID == "" && c.Auth.CompartmentID != "" {
171+
if len(c.CompartmentID) == 0 && len(c.Auth.CompartmentID) > 0 {
131172
zap.S().Warn("cloud-provider config: \"auth.compartment\" is DEPRECATED and will be removed in a later release. Please set \"compartment\".")
132173
c.CompartmentID = c.Auth.CompartmentID
133174
}
134-
135-
if c.Auth.Passphrase == "" && c.Auth.PrivateKeyPassphrase != "" {
136-
zap.S().Warn("cloud-provider config: \"auth.key_passphrase\" is DEPRECATED and will be removed in a later release. Please set \"auth.passphrase\".")
137-
c.Auth.Passphrase = c.Auth.PrivateKeyPassphrase
175+
if c.Auth.UseInstancePrincipals {
176+
zap.S().Warn("cloud-provider config: \"auth.useInstancePrincipals\" is DEPRECATED and will be removed in a later release. Please set \"useInstancePrincipals\".")
177+
c.UseInstancePrincipals = true
138178
}
139179

140-
if c.Auth.UseInstancePrincipals == true {
141-
zap.S().Warn("cloud-provider config: \"auth.useInstancePrincipals\" is DEPRECATED and will be removed in a later release. Please set \"auth.useInstancePrincipals\".")
142-
c.UseInstancePrincipals = true
180+
if len(c.RegionKey) == 0 {
181+
if len(c.Auth.RegionKey) > 0 {
182+
zap.S().Warn("cloud-provider config: \"auth.RegionKey\" is DEPRECATED and will be removed in a later release. Please set \"RegionKey\".")
183+
c.RegionKey = c.Auth.RegionKey
184+
} else {
185+
meta, err := c.metadataSvc.Get()
186+
if err != nil {
187+
zap.S().Warn("cloud-provider config: Unable to access metadata on instance. Will not be able to complete configuration if items are missing")
188+
return
189+
}
190+
c.RegionKey = meta.Region
191+
}
143192
}
144193
}
145194

@@ -160,6 +209,8 @@ func ReadConfig(r io.Reader) (*Config, error) {
160209
return nil, errors.Wrap(err, "unmarshalling cloud-provider config")
161210
}
162211

212+
cfg.metadataSvc = metadata.New()
213+
cfg.Auth.metadataSvc = cfg.metadataSvc
163214
// Ensure defaults are correctly set
164215
cfg.Complete()
165216

0 commit comments

Comments
 (0)