Skip to content

Commit 298725d

Browse files
committed
chore: use f-string
1 parent 397d06b commit 298725d

16 files changed

+42
-60
lines changed

examples/annotate_deployment.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ def main():
6262
time.sleep(1)
6363
before_annotating = apps_v1_api.read_namespaced_deployment(
6464
'deploy-nginx', 'default')
65-
print('Before annotating, annotations: %s' %
66-
before_annotating.metadata.annotations)
65+
print(f"Before annotating, annotations: {before_annotating.metadata.annotations}")
6766

6867
annotations = [
6968
{
@@ -80,8 +79,7 @@ def main():
8079
time.sleep(1)
8180
after_annotating = apps_v1_api.read_namespaced_deployment(
8281
name='deploy-nginx', namespace='default')
83-
print('After annotating, annotations: %s' %
84-
after_annotating.metadata.annotations)
82+
print(f"After annotating, annotations: {after_annotating.metadata.annotations}")
8583

8684

8785
if __name__ == "__main__":

examples/api_discovery.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ def main():
2727
config.load_kube_config()
2828

2929
print("Supported APIs (* is preferred version):")
30-
print("%-40s %s" %
31-
("core", ",".join(client.CoreApi().get_api_versions().versions)))
30+
print(f"{'core':<40} {','.join(client.CoreApi().get_api_versions().versions)}")
3231
for api in client.ApisApi().get_api_versions().groups:
3332
versions = []
3433
for v in api.versions:
@@ -38,7 +37,7 @@ def main():
3837
name += "*"
3938
name += v.version
4039
versions.append(name)
41-
print("%-40s %s" % (api.name, ",".join(versions)))
40+
print(f"{api.name:<40} {','.join(versions)}")
4241

4342

4443
if __name__ == '__main__':

examples/apply_from_directory.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from kubernetes import client,config,utils
1+
from kubernetes import client, config, utils
22

33
def main():
44
config.load_kube_config()

examples/apply_from_single_file.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from kubernetes import client,config,utils
1+
from kubernetes import client, config, utils
22

33
def main():
44
config.load_kube_config()
55
k8s_client = client.ApiClient()
6-
yaml_file = 'examples/configmap-demo-pod.yml'
6+
yaml_file = 'examples/yaml_dir/configmap-demo-pod.yml'
77
utils.create_from_yaml(k8s_client,yaml_file,verbose=True)
88

99
if __name__ == "__main__":

examples/cluster_scoped_custom_object.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,7 @@ def main():
100100
plural="crontabs",
101101
)
102102
print("%s\t\t%s" % ("NAME", "CRON-SPEC"))
103-
print(
104-
"%s\t%s\n" %
105-
(resource["metadata"]["name"],
106-
resource["spec"]["cronSpec"]))
103+
print(f"{resource['metadata']['name']}\t{resource['spec']['cronSpec']}\n")
107104

108105
# patch the `spec.cronSpec` field of the custom resource
109106
patched_resource = api.patch_cluster_custom_object(
@@ -115,10 +112,7 @@ def main():
115112
)
116113
print("[INFO] Custom resource `test-crontab` patched to update the cronSpec schedule!\n")
117114
print("%s\t\t%s" % ("NAME", "PATCHED-CRON-SPEC"))
118-
print(
119-
"%s\t%s\n" %
120-
(patched_resource["metadata"]["name"],
121-
patched_resource["spec"]["cronSpec"]))
115+
print(f"{patched_resource['metadata']['name']}\t{patched_resource['spec']['cronSpec']}\n")
122116

123117
# patch the `metadata.labels` field of the custom resource
124118
patched_resource = api.patch_cluster_custom_object(
@@ -130,10 +124,7 @@ def main():
130124
)
131125
print("[INFO] Custom resource `test-crontab` patched to apply new metadata labels!\n")
132126
print("%s\t\t%s" % ("NAME", "PATCHED_LABELS"))
133-
print(
134-
"%s\t%s\n" %
135-
(patched_resource["metadata"]["name"],
136-
patched_resource["metadata"]["labels"]))
127+
print(f"{patched_resource['metadata']['name']}\t{patched_resource['metadata']['labels']}\n")
137128

138129
# delete the custom resource "test-crontab"
139130
api.delete_cluster_custom_object(

examples/deployment_create.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def main():
3434
k8s_apps_v1 = client.AppsV1Api()
3535
resp = k8s_apps_v1.create_namespaced_deployment(
3636
body=dep, namespace="default")
37-
print("Deployment created. status='%s'" % resp.metadata.name)
37+
print(f"Deployment created. Status='{resp.metadata.name}'")
3838

3939

4040
if __name__ == '__main__':

examples/in_cluster_config.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ def main():
5858
print("Listing pods with their IPs:")
5959
ret = v1.list_pod_for_all_namespaces(watch=False)
6060
for i in ret.items:
61-
print("%s\t%s\t%s" %
62-
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
61+
print(f"{i.status.pod_ip}\t{i.metadata.namespace}\t{i.metadata.name}")
6362

6463

6564
if __name__ == '__main__':

examples/job_crud.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828

2929
def create_job_object():
30-
# Configureate Pod template container
30+
# Configure Pod template container
3131
container = client.V1Container(
3232
name="pi",
3333
image="perl",
@@ -54,7 +54,7 @@ def create_job(api_instance, job):
5454
api_response = api_instance.create_namespaced_job(
5555
body=job,
5656
namespace="default")
57-
print("Job created. status='%s'" % str(api_response.status))
57+
print(f"Job created. status='{str(api_response.status)}'")
5858
get_job_status(api_instance)
5959

6060

@@ -68,7 +68,7 @@ def get_job_status(api_instance):
6868
api_response.status.failed is not None:
6969
job_completed = True
7070
sleep(1)
71-
print("Job status='%s'" % str(api_response.status))
71+
print(f"Job status='{str(api_response.status)}'")
7272

7373

7474
def update_job(api_instance, job):
@@ -78,7 +78,7 @@ def update_job(api_instance, job):
7878
name=JOB_NAME,
7979
namespace="default",
8080
body=job)
81-
print("Job updated. status='%s'" % str(api_response.status))
81+
print(f"Job updated. status='{str(api_response.status)}'")
8282

8383

8484
def delete_job(api_instance):
@@ -88,7 +88,7 @@ def delete_job(api_instance):
8888
body=client.V1DeleteOptions(
8989
propagation_policy='Foreground',
9090
grace_period_seconds=5))
91-
print("Job deleted. status='%s'" % str(api_response.status))
91+
print(f"Job deleted. status='{str(api_response.status)}'")
9292

9393

9494
def main():

examples/multiple_clusters.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@ def main():
4343

4444
print("\nList of pods on %s:" % cluster1)
4545
for i in client1.list_pod_for_all_namespaces().items:
46-
print("%s\t%s\t%s" %
47-
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
46+
print(f"{i.status.pod_ip}\t{i.metadata.namespace}\t{i.metadata.name}")
4847

49-
print("\n\nList of pods on %s:" % cluster2)
48+
print(f"\n\nList of pods on {cluster2}:")
5049
for i in client2.list_pod_for_all_namespaces().items:
51-
print("%s\t%s\t%s" %
52-
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
50+
print(f"{i.status.pod_ip}\t{i.metadata.namespace}\t{i.metadata.name}")
5351

5452

5553
if __name__ == '__main__':

examples/node_labels.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def main():
4444
# Patching the node labels
4545
for node in node_list.items:
4646
api_response = api_instance.patch_node(node.metadata.name, body)
47-
print("%s\t%s" % (node.metadata.name, node.metadata.labels))
47+
print(f"{node.metadata.name}\t{node.metadata.labels}")
4848

4949

5050
if __name__ == '__main__':

examples/out_of_cluster_config.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
"""
16-
Shows how to load a Kubernetes config from outside of the cluster.
16+
Shows how to load a Kubernetes config from outside the cluster.
1717
"""
1818

1919
from kubernetes import client, config
@@ -29,8 +29,7 @@ def main():
2929
print("Listing pods with their IPs:")
3030
ret = v1.list_pod_for_all_namespaces(watch=False)
3131
for i in ret.items:
32-
print("%s\t%s\t%s" %
33-
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
32+
print(f"{i.status.pod_ip}\t{i.metadata.namespace}\t{i.metadata.name}")
3433

3534

3635
if __name__ == '__main__':

examples/pick_kube_config_context.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def main():
3737
# utility
3838
config.load_kube_config(context=option)
3939

40-
print("Active host is %s" % configuration.Configuration().host)
40+
print(f"Active host is {configuration.Configuration().host}")
4141

4242
v1 = client.CoreV1Api()
4343
print("Listing pods with their IPs:")

examples/pod_config_list.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def main():
3838
# utility
3939
config.load_kube_config(context=option)
4040

41-
print("Active host is %s" % configuration.Configuration().host)
41+
print(f"Active host is {configuration.Configuration().host}")
4242

4343
v1 = client.CoreV1Api()
4444
print("Listing pods with their IPs:")

examples/pod_exec.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ def exec_commands(api_instance):
3333
namespace='default')
3434
except ApiException as e:
3535
if e.status != 404:
36-
print("Unknown error: %s" % e)
36+
print(f"Unknown error: {e}")
3737
exit(1)
3838

3939
if not resp:
40-
print("Pod %s does not exist. Creating it..." % name)
40+
print(f"Pod {name} does not exist. Creating it...")
4141
pod_manifest = {
4242
'apiVersion': 'v1',
4343
'kind': 'Pod',
@@ -98,22 +98,22 @@ def exec_commands(api_instance):
9898
while resp.is_open():
9999
resp.update(timeout=1)
100100
if resp.peek_stdout():
101-
print("STDOUT: %s" % resp.read_stdout())
101+
print(f"STDOUT: {resp.read_stdout()}")
102102
if resp.peek_stderr():
103-
print("STDERR: %s" % resp.read_stderr())
103+
print(f"STDERR: {resp.read_stderr()}")
104104
if commands:
105105
c = commands.pop(0)
106-
print("Running command... %s\n" % c)
106+
print(f"Running command... {c}\n")
107107
resp.write_stdin(c + "\n")
108108
else:
109109
break
110110

111111
resp.write_stdin("date\n")
112112
sdate = resp.readline_stdout(timeout=3)
113-
print("Server date command returns: %s" % sdate)
113+
print(f"Server date command returns: {sdate}")
114114
resp.write_stdin("whoami\n")
115115
user = resp.readline_stdout(timeout=3)
116-
print("Server user is: %s" % user)
116+
print(f"Server user is: {user}")
117117
resp.close()
118118

119119

examples/pod_portforward.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ def portforward_commands(api_instance):
6666
namespace='default')
6767
except ApiException as e:
6868
if e.status != 404:
69-
print("Unknown error: %s" % e)
69+
print(f"Unknown error: {e}")
7070
exit(1)
7171

7272
if not resp:
73-
print("Pod %s does not exist. Creating it..." % name)
73+
print(f"Pod {name} does not exist. Creating it...")
7474
pod_manifest = {
7575
'apiVersion': 'v1',
7676
'kind': 'Pod',
@@ -119,7 +119,7 @@ def portforward_commands(api_instance):
119119
if error is None:
120120
print("No port forward errors on port 80.")
121121
else:
122-
print("Port 80 has the following error: %s" % error)
122+
print(f"Port 80 has the following error: {error}")
123123

124124
# Monkey patch socket.create_connection which is used by http.client and
125125
# urllib.request. The same can be done with urllib3.util.connection.create_connection
@@ -147,10 +147,10 @@ def kubernetes_create_connection(address, *args, **kwargs):
147147
break
148148
else:
149149
raise RuntimeError(
150-
"Unable to find service port: %s" % port)
150+
f"Unable to find service port: {port}")
151151
label_selector = []
152152
for key, value in service.spec.selector.items():
153-
label_selector.append("%s=%s" % (key, value))
153+
label_selector.append(f"{key}={value}")
154154
pods = api_instance.list_namespaced_pod(
155155
namespace, label_selector=",".join(label_selector)
156156
)
@@ -168,11 +168,10 @@ def kubernetes_create_connection(address, *args, **kwargs):
168168
break
169169
else:
170170
raise RuntimeError(
171-
"Unable to find service port name: %s" % port)
171+
f"Unable to find service port name: {port}")
172172
elif dns_name[1] != 'pod':
173173
raise RuntimeError(
174-
"Unsupported resource type: %s" %
175-
dns_name[1])
174+
f"Unsupported resource type: {dns_name[1]}")
176175
pf = portforward(api_instance.connect_get_namespaced_pod_portforward,
177176
name, namespace, ports=str(port))
178177
return pf.socket(port)
@@ -181,10 +180,10 @@ def kubernetes_create_connection(address, *args, **kwargs):
181180
# Access the nginx http server using the
182181
# "<pod-name>.pod.<namespace>.kubernetes" dns name.
183182
response = urllib_request.urlopen(
184-
'http://%s.pod.default.kubernetes' % name)
183+
f'http://{name}.pod.default.kubernetes')
185184
html = response.read().decode('utf-8')
186185
response.close()
187-
print('Status Code: %s' % response.code)
186+
print(f'Status Code: {response.code}')
188187
print(html)
189188

190189

examples/remote_cluster.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ def main():
5252
print("Listing pods with their IPs:")
5353
ret = v1.list_pod_for_all_namespaces(watch=False)
5454
for i in ret.items:
55-
print("%s\t%s\t%s" %
56-
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
55+
print(f"{i.status.pod_ip}\t{i.metadata.namespace}\t{i.metadata.name}")
5756

5857

5958
if __name__ == '__main__':

0 commit comments

Comments
 (0)