Skip to content

Commit ddc6f09

Browse files
authored
Merge pull request #487 from mplsgrant/update-auth-command
update auth command
2 parents 6850b47 + a6fc4e1 commit ddc6f09

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

src/warnet/main.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,37 +136,62 @@ def init():
136136
@click.argument("kube_config", type=str)
137137
def auth(kube_config: str) -> None:
138138
"""
139-
Authorize access to a warnet cluster using a kube config file
139+
Authenticate with a warnet cluster using a kube config file
140140
"""
141141
try:
142142
current_kubeconfig = os.environ.get("KUBECONFIG", os.path.expanduser("~/.kube/config"))
143143
combined_kubeconfig = (
144144
f"{current_kubeconfig}:{kube_config}" if current_kubeconfig else kube_config
145145
)
146146
os.environ["KUBECONFIG"] = combined_kubeconfig
147-
command = "kubectl config view --flatten"
148-
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
147+
with open(kube_config) as file:
148+
content = yaml.safe_load(file)
149+
for elem in content:
150+
print(elem)
151+
content["clusters"][0]
152+
user = content["users"][0]
153+
user_name = user["name"]
154+
user_token = user["user"]["token"]
155+
content["contexts"][0]
156+
flatten_cmd = "kubectl config view --flatten"
157+
result_flatten = subprocess.run(
158+
flatten_cmd, shell=True, check=True, capture_output=True, text=True
159+
)
149160
except subprocess.CalledProcessError as e:
150161
print("Error occurred while executing kubectl config view --flatten:")
151162
print(e.stderr)
152163
sys.exit(1)
153164

154-
if result.returncode == 0:
165+
if result_flatten.returncode == 0:
155166
with open(current_kubeconfig, "w") as file:
156-
file.write(result.stdout)
167+
file.write(result_flatten.stdout)
157168
print(f"Authorization file written to: {current_kubeconfig}")
158169
else:
159170
print("Could not create authorization file")
160-
print(result.stderr)
161-
sys.exit(result.returncode)
171+
print(result_flatten.stderr)
172+
sys.exit(result_flatten.returncode)
173+
174+
try:
175+
update_cmd = f"kubectl config set-credentials {user_name} --token {user_token}"
176+
result_update = subprocess.run(
177+
update_cmd, shell=True, check=True, capture_output=True, text=True
178+
)
179+
if result_update.returncode != 0:
180+
print("Could not update authorization file")
181+
print(result_flatten.stderr)
182+
sys.exit(result_flatten.returncode)
183+
except subprocess.CalledProcessError as e:
184+
print("Error occurred while executing kubectl config view --flatten:")
185+
print(e.stderr)
186+
sys.exit(1)
162187

163188
with open(current_kubeconfig) as file:
164189
contents = yaml.safe_load(file)
165190
print("\nUse the following command to switch to a new user:")
166191
print(" kubectl config use-context [user]\n")
167192
print("Available users:")
168-
for context in contents["contexts"]:
169-
print(f" {context['name']}")
193+
for c in contents["contexts"]:
194+
print(f" {c['name']}")
170195

171196

172197
if __name__ == "__main__":

src/warnet/network.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,20 @@ def should_copy(item: Path) -> bool:
8888

8989
print(f"Finished copying files to {target_dir}")
9090

91+
9192
def copy_network_defaults(directory: Path):
9293
"""Create the project structure for a warnet project's network"""
9394
copy_defaults(directory, WAR_NETWORK_DIR, WAR_NETWORK_FILES.joinpath(), [])
9495

96+
9597
def copy_scenario_defaults(directory: Path):
9698
"""Create the project structure for a warnet project's scenarios"""
97-
copy_defaults(directory, WAR_SCENARIOS_DIR, WAR_SCENARIOS_FILES.joinpath(), ["__init__.py", "__pycache__", "commander.py"])
99+
copy_defaults(
100+
directory,
101+
WAR_SCENARIOS_DIR,
102+
WAR_SCENARIOS_FILES.joinpath(),
103+
["__init__.py", "__pycache__", "commander.py"],
104+
)
98105

99106

100107
@network.command()

0 commit comments

Comments
 (0)