43
43
from charms .reactive .helpers import data_changed , any_file_changed
44
44
from charms .kubernetes .common import get_version
45
45
from charms .kubernetes .common import retry
46
- from charms .kubernetes .flagmanager import FlagManager
47
46
48
47
from charms .layer import tls_client
49
48
@@ -172,11 +171,6 @@ def migrate_from_pre_snaps():
172
171
hookenv .log ("Removing file: " + file )
173
172
os .remove (file )
174
173
175
- # clear the flag managers
176
- FlagManager ('kube-apiserver' ).destroy_all ()
177
- FlagManager ('kube-controller-manager' ).destroy_all ()
178
- FlagManager ('kube-scheduler' ).destroy_all ()
179
-
180
174
181
175
def install_snaps ():
182
176
channel = hookenv .config ('channel' )
@@ -228,15 +222,10 @@ def configure_cni(cni):
228
222
@when_not ('authentication.setup' )
229
223
def setup_leader_authentication ():
230
224
'''Setup basic authentication and token access for the cluster.'''
231
- api_opts = FlagManager ('kube-apiserver' )
232
- controller_opts = FlagManager ('kube-controller-manager' )
233
-
234
225
service_key = '/root/cdk/serviceaccount.key'
235
226
basic_auth = '/root/cdk/basic_auth.csv'
236
227
known_tokens = '/root/cdk/known_tokens.csv'
237
228
238
- api_opts .add ('basic-auth-file' , basic_auth )
239
- api_opts .add ('token-auth-file' , known_tokens )
240
229
hookenv .status_set ('maintenance' , 'Rendering authentication templates.' )
241
230
242
231
keys = [service_key , basic_auth , known_tokens ]
@@ -257,9 +246,6 @@ def setup_leader_authentication():
257
246
check_call (cmd )
258
247
remove_state ('reconfigure.authentication.setup' )
259
248
260
- api_opts .add ('service-account-key-file' , service_key )
261
- controller_opts .add ('service-account-private-key-file' , service_key )
262
-
263
249
# read service account key for syndication
264
250
leader_data = {}
265
251
for f in [known_tokens , basic_auth , service_key ]:
@@ -294,13 +280,6 @@ def setup_non_leader_authentication():
294
280
return
295
281
296
282
hookenv .status_set ('maintenance' , 'Rendering authentication templates.' )
297
- api_opts = FlagManager ('kube-apiserver' )
298
- api_opts .add ('basic-auth-file' , basic_auth )
299
- api_opts .add ('token-auth-file' , known_tokens )
300
- api_opts .add ('service-account-key-file' , service_key )
301
-
302
- controller_opts = FlagManager ('kube-controller-manager' )
303
- controller_opts .add ('service-account-private-key-file' , service_key )
304
283
305
284
remove_state ('kubernetes-master.components.started' )
306
285
set_state ('authentication.setup' )
@@ -400,7 +379,7 @@ def start_master(etcd):
400
379
handle_etcd_relation (etcd )
401
380
402
381
# Add CLI options to all components
403
- configure_apiserver ()
382
+ configure_apiserver (etcd )
404
383
configure_controller_manager ()
405
384
configure_scheduler ()
406
385
@@ -768,8 +747,9 @@ def on_config_allow_privileged_change():
768
747
769
748
@when ('config.changed.api-extra-args' )
770
749
@when ('kubernetes-master.components.started' )
771
- def on_config_api_extra_args_change ():
772
- configure_apiserver ()
750
+ @when ('etcd.available' )
751
+ def on_config_api_extra_args_change (etcd ):
752
+ configure_apiserver (etcd )
773
753
774
754
775
755
@when ('config.changed.controller-manager-extra-args' )
@@ -957,9 +937,9 @@ def get_kubernetes_service_ip():
957
937
def handle_etcd_relation (reldata ):
958
938
''' Save the client credentials and set appropriate daemon flags when
959
939
etcd declares itself as available'''
960
- connection_string = reldata .get_connection_string ()
961
940
# Define where the etcd tls files will be kept.
962
941
etcd_dir = '/root/cdk/etcd'
942
+
963
943
# Create paths to the etcd client ca, key, and cert file locations.
964
944
ca = os .path .join (etcd_dir , 'client-ca.pem' )
965
945
key = os .path .join (etcd_dir , 'client-key.pem' )
@@ -968,85 +948,45 @@ def handle_etcd_relation(reldata):
968
948
# Save the client credentials (in relation data) to the paths provided.
969
949
reldata .save_client_credentials (key , cert , ca )
970
950
971
- api_opts = FlagManager ('kube-apiserver' )
972
951
973
- # Never use stale data, always prefer whats coming in during context
974
- # building. if its stale, its because whats in unitdata is stale
975
- data = api_opts .data
976
- if data .get ('etcd-servers-strict' ) or data .get ('etcd-servers' ):
977
- api_opts .destroy ('etcd-cafile' )
978
- api_opts .destroy ('etcd-keyfile' )
979
- api_opts .destroy ('etcd-certfile' )
980
- api_opts .destroy ('etcd-servers' , strict = True )
981
- api_opts .destroy ('etcd-servers' )
952
+ def parse_extra_args (config_key ):
953
+ elements = hookenv .config ().get (config_key , '' ).split ()
954
+ args = {}
955
+
956
+ for element in elements :
957
+ if '=' in element :
958
+ key , _ , value = element .partition ('=' )
959
+ args [key ] = value
960
+ else :
961
+ args [element ] = 'true'
982
962
983
- # Set the apiserver flags in the options manager
984
- api_opts .add ('etcd-cafile' , ca )
985
- api_opts .add ('etcd-keyfile' , key )
986
- api_opts .add ('etcd-certfile' , cert )
987
- api_opts .add ('etcd-servers' , connection_string , strict = True )
963
+ return args
988
964
989
965
990
- def get_config_args ( key ):
966
+ def configure_kubernetes_service ( service , base_args , extra_args_key ):
991
967
db = unitdata .kv ()
992
- old_config_args = db .get (key , [])
993
- # We have to convert them to tuples becuase we use sets
994
- old_config_args = [tuple (i ) for i in old_config_args ]
995
- new_config_args = []
996
- new_config_arg_names = []
997
- for arg in hookenv .config ().get (key , '' ).split ():
998
- new_config_arg_names .append (arg .split ('=' , 1 )[0 ])
999
- if len (arg .split ('=' , 1 )) == 1 : # handle flags ie. --profiling
1000
- new_config_args .append (tuple ([arg , 'true' ]))
1001
- else :
1002
- new_config_args .append (tuple (arg .split ('=' , 1 )))
1003
-
1004
- hookenv .log ('Handling "%s" option.' % key )
1005
- hookenv .log ('Old arguments: {}' .format (old_config_args ))
1006
- hookenv .log ('New arguments: {}' .format (new_config_args ))
1007
- if set (new_config_args ) == set (old_config_args ):
1008
- return (new_config_args , [])
1009
- # Store new args
1010
- db .set (key , new_config_args )
1011
- to_add = set (new_config_args )
1012
- to_remove = set (old_config_args ) - set (new_config_args )
1013
- # Extract option names only
1014
- to_remove = [i [0 ] for i in to_remove if i [0 ] not in new_config_arg_names ]
1015
- return (to_add , to_remove )
1016
968
969
+ prev_args_key = 'kubernetes-master.prev_args.' + service
970
+ prev_args = db .get (prev_args_key ) or {}
1017
971
1018
- def configure_kubernetes_service (service , base_args , extra_args_key ):
1019
- # Handle api-extra-args config option
1020
- to_add , to_remove = get_config_args (extra_args_key )
1021
-
1022
- flag_manager = FlagManager (service )
1023
-
1024
- # Remove arguments that are no longer provided as config option
1025
- # this allows them to be reverted to charm defaults
1026
- for arg in to_remove :
1027
- hookenv .log ('Removing option: {}' .format (arg ))
1028
- flag_manager .destroy (arg )
1029
- # We need to "unset" options by setting their value to "null" string
1030
- cmd = ['snap' , 'set' , service , '{}=null' .format (arg )]
1031
- check_call (cmd )
972
+ extra_args = parse_extra_args (extra_args_key )
1032
973
1033
- # Add base arguments
974
+ args = {}
975
+ for arg in prev_args :
976
+ # remove previous args by setting to null
977
+ args [arg ] = 'null'
1034
978
for k , v in base_args .items ():
1035
- flag_manager .add (k , v , strict = True )
1036
-
1037
- # Add operator-provided arguments, this allows operators
1038
- # to override defaults
1039
- for arg in to_add :
1040
- hookenv .log ('Adding option: {} {}' .format (arg [0 ], arg [1 ]))
1041
- # Make sure old value is gone
1042
- flag_manager .destroy (arg [0 ])
1043
- flag_manager .add (arg [0 ], arg [1 ], strict = True )
979
+ args [k ] = v
980
+ for k , v in extra_args .items ():
981
+ args [k ] = v
1044
982
1045
- cmd = ['snap' , 'set' , service ] + flag_manager . to_s (). split ( ' ' )
983
+ cmd = ['snap' , 'set' , service ] + [ '%s=%s' % item for item in args . items ()]
1046
984
check_call (cmd )
1047
985
986
+ db .set (prev_args_key , args )
1048
987
1049
- def configure_apiserver ():
988
+
989
+ def configure_apiserver (etcd ):
1050
990
api_opts = {}
1051
991
1052
992
# Get the tls paths from the layer data.
@@ -1078,6 +1018,20 @@ def configure_apiserver():
1078
1018
api_opts ['insecure-port' ] = '8080'
1079
1019
api_opts ['storage-backend' ] = 'etcd2' # FIXME: add etcd3 support
1080
1020
1021
+ api_opts ['basic-auth-file' ] = '/root/cdk/basic_auth.csv'
1022
+ api_opts ['token-auth-file' ] = '/root/cdk/known_tokens.csv'
1023
+ api_opts ['service-account-key-file' ] = '/root/cdk/serviceaccount.key'
1024
+
1025
+ etcd_dir = '/root/cdk/etcd'
1026
+ etcd_ca = os .path .join (etcd_dir , 'client-ca.pem' )
1027
+ etcd_key = os .path .join (etcd_dir , 'client-key.pem' )
1028
+ etcd_cert = os .path .join (etcd_dir , 'client-cert.pem' )
1029
+
1030
+ api_opts ['etcd-cafile' ] = etcd_ca
1031
+ api_opts ['etcd-keyfile' ] = etcd_key
1032
+ api_opts ['etcd-certfile' ] = etcd_cert
1033
+ api_opts ['etcd-servers' ] = etcd .get_connection_string ()
1034
+
1081
1035
admission_control = [
1082
1036
'Initializers' ,
1083
1037
'NamespaceLifecycle' ,
@@ -1120,6 +1074,9 @@ def configure_controller_manager():
1120
1074
controller_opts ['logtostderr' ] = 'true'
1121
1075
controller_opts ['master' ] = 'http://127.0.0.1:8080'
1122
1076
1077
+ controller_opts ['service-account-private-key-file' ] = \
1078
+ '/root/cdk/serviceaccount.key'
1079
+
1123
1080
configure_kubernetes_service ('kube-controller-manager' , controller_opts ,
1124
1081
'controller-manager-extra-args' )
1125
1082
0 commit comments