Skip to content

Commit 43556be

Browse files
committed
Enhance metadata fetching functions.
Introduce Get-InstanceMetadata which can be used to fetch non-"attribute" metadata values.
1 parent e476ab6 commit 43556be

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

cluster/gce/windows/common.psm1

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,13 @@ function ShouldWrite-File {
6565

6666
# Returns the GCE instance metadata value for $Key. If the key is not present
6767
# in the instance metadata returns $Default if set, otherwise returns $null.
68-
function Get-InstanceMetadataValue {
68+
function Get-InstanceMetadata {
6969
param (
7070
[parameter(Mandatory=$true)] [string]$Key,
7171
[parameter(Mandatory=$false)] [string]$Default
7272
)
7373

74-
$url = ("http://metadata.google.internal/computeMetadata/v1/instance/" +
75-
"attributes/$Key")
74+
$url = "http://metadata.google.internal/computeMetadata/v1/instance/$Key"
7675
try {
7776
$client = New-Object Net.WebClient
7877
$client.Headers.Add('Metadata-Flavor', 'Google')
@@ -89,6 +88,18 @@ function Get-InstanceMetadataValue {
8988
}
9089
}
9190

91+
# Returns the GCE instance metadata value for $Key where key is an "attribute"
92+
# of the instance. If the key is not present in the instance metadata returns
93+
# $Default if set, otherwise returns $null.
94+
function Get-InstanceMetadataAttribute {
95+
param (
96+
[parameter(Mandatory=$true)] [string]$Key,
97+
[parameter(Mandatory=$false)] [string]$Default
98+
)
99+
100+
return Get-InstanceMetadata "attributes/$Key" $Default
101+
}
102+
92103
function Validate-SHA1 {
93104
param(
94105
[parameter(Mandatory=$true)] [string]$Hash,

cluster/gce/windows/configure.ps1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ $ErrorActionPreference = 'Stop'
2727
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
2828
$ProgressPreference = 'SilentlyContinue'
2929

30-
# Returns the GCE instance metadata value for $Key. If the key is not present
31-
# in the instance metadata returns $Default if set, otherwise returns $null.
32-
function Get-InstanceMetadataValue {
30+
# Returns the GCE instance metadata value for $Key where key is an "attribute"
31+
# of the instance. If the key is not present in the instance metadata returns
32+
# $Default if set, otherwise returns $null.
33+
function Get-InstanceMetadataAttribute {
3334
param (
3435
[parameter(Mandatory=$true)] [string]$Key,
3536
[parameter(Mandatory=$false)] [string]$Default
@@ -63,7 +64,7 @@ function FetchAndImport-ModuleFromMetadata {
6364
[parameter(Mandatory=$true)] [string]$Filename
6465
)
6566

66-
$module = Get-InstanceMetadataValue $MetadataKey
67+
$module = Get-InstanceMetadataAttribute $MetadataKey
6768
if (Test-Path C:\$Filename) {
6869
if (-not $REDO_STEPS) {
6970
Log-Output "Skip: C:\$Filename already exists, not overwriting"
@@ -81,7 +82,7 @@ try {
8182
# Don't use FetchAndImport-ModuleFromMetadata for common.psm1 - the common
8283
# module includes variables and functions that any other function may depend
8384
# on.
84-
$module = Get-InstanceMetadataValue 'common-psm1'
85+
$module = Get-InstanceMetadataAttribute 'common-psm1'
8586
New-Item -ItemType file -Force C:\common.psm1 | Out-Null
8687
Set-Content C:\common.psm1 $module
8788
Import-Module -Force C:\common.psm1

cluster/gce/windows/k8s-node-setup.psm1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,15 @@ function Add_GceMetadataServerRoute {
135135
function Fetch-KubeEnv {
136136
# Testing / debugging:
137137
# First:
138-
# ${kube_env} = Get-InstanceMetadataValue 'kube-env'
138+
# ${kube_env} = Get-InstanceMetadataAttribute 'kube-env'
139139
# or:
140140
# ${kube_env} = [IO.File]::ReadAllText(".\kubeEnv.txt")
141141
# ${kube_env_table} = ConvertFrom-Yaml ${kube_env}
142142
# ${kube_env_table}
143143
# ${kube_env_table}.GetType()
144144

145145
# The type of kube_env is a powershell String.
146-
$kube_env = Get-InstanceMetadataValue 'kube-env'
146+
$kube_env = Get-InstanceMetadataAttribute 'kube-env'
147147
$kube_env_table = ConvertFrom-Yaml ${kube_env}
148148
return ${kube_env_table}
149149
}
@@ -882,7 +882,7 @@ function Configure-Kubelet {
882882
# The Kubelet config is built by build-kubelet-config() in
883883
# cluster/gce/util.sh, and stored in the metadata server under the
884884
# 'kubelet-config' key.
885-
$kubelet_config = Get-InstanceMetadataValue 'kubelet-config'
885+
$kubelet_config = Get-InstanceMetadataAttribute 'kubelet-config'
886886
Set-Content ${env:KUBELET_CONFIG} $kubelet_config
887887
Log-Output "Kubelet config:`n$(Get-Content -Raw ${env:KUBELET_CONFIG})"
888888
}

cluster/gce/windows/testonly/install-ssh.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function Setup_WriteSshKeysScript {
106106

107107
# Fetch helper module for manipulating Windows user profiles.
108108
if (ShouldWrite-File $USER_PROFILE_MODULE) {
109-
$module = Get-InstanceMetadataValue 'user-profile-psm1'
109+
$module = Get-InstanceMetadataAttribute 'user-profile-psm1'
110110
New-Item -ItemType file -Force $USER_PROFILE_MODULE | Out-Null
111111
Set-Content $USER_PROFILE_MODULE $module
112112
}

0 commit comments

Comments
 (0)