Enhance metadata fetching functions.

Introduce Get-InstanceMetadata which can be used to fetch
non-"attribute" metadata values.
This commit is contained in:
Peter Hornyack 2019-02-28 17:17:02 -08:00
parent e476ab63cc
commit 43556be50e
4 changed files with 24 additions and 12 deletions

View File

@ -65,14 +65,13 @@ function ShouldWrite-File {
# Returns the GCE instance metadata value for $Key. If the key is not present # Returns the GCE instance metadata value for $Key. If the key is not present
# in the instance metadata returns $Default if set, otherwise returns $null. # in the instance metadata returns $Default if set, otherwise returns $null.
function Get-InstanceMetadataValue { function Get-InstanceMetadata {
param ( param (
[parameter(Mandatory=$true)] [string]$Key, [parameter(Mandatory=$true)] [string]$Key,
[parameter(Mandatory=$false)] [string]$Default [parameter(Mandatory=$false)] [string]$Default
) )
$url = ("http://metadata.google.internal/computeMetadata/v1/instance/" + $url = "http://metadata.google.internal/computeMetadata/v1/instance/$Key"
"attributes/$Key")
try { try {
$client = New-Object Net.WebClient $client = New-Object Net.WebClient
$client.Headers.Add('Metadata-Flavor', 'Google') $client.Headers.Add('Metadata-Flavor', 'Google')
@ -89,6 +88,18 @@ function Get-InstanceMetadataValue {
} }
} }
# Returns the GCE instance metadata value for $Key where key is an "attribute"
# of the instance. If the key is not present in the instance metadata returns
# $Default if set, otherwise returns $null.
function Get-InstanceMetadataAttribute {
param (
[parameter(Mandatory=$true)] [string]$Key,
[parameter(Mandatory=$false)] [string]$Default
)
return Get-InstanceMetadata "attributes/$Key" $Default
}
function Validate-SHA1 { function Validate-SHA1 {
param( param(
[parameter(Mandatory=$true)] [string]$Hash, [parameter(Mandatory=$true)] [string]$Hash,

View File

@ -27,9 +27,10 @@ $ErrorActionPreference = 'Stop'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$ProgressPreference = 'SilentlyContinue' $ProgressPreference = 'SilentlyContinue'
# Returns the GCE instance metadata value for $Key. If the key is not present # Returns the GCE instance metadata value for $Key where key is an "attribute"
# in the instance metadata returns $Default if set, otherwise returns $null. # of the instance. If the key is not present in the instance metadata returns
function Get-InstanceMetadataValue { # $Default if set, otherwise returns $null.
function Get-InstanceMetadataAttribute {
param ( param (
[parameter(Mandatory=$true)] [string]$Key, [parameter(Mandatory=$true)] [string]$Key,
[parameter(Mandatory=$false)] [string]$Default [parameter(Mandatory=$false)] [string]$Default
@ -63,7 +64,7 @@ function FetchAndImport-ModuleFromMetadata {
[parameter(Mandatory=$true)] [string]$Filename [parameter(Mandatory=$true)] [string]$Filename
) )
$module = Get-InstanceMetadataValue $MetadataKey $module = Get-InstanceMetadataAttribute $MetadataKey
if (Test-Path C:\$Filename) { if (Test-Path C:\$Filename) {
if (-not $REDO_STEPS) { if (-not $REDO_STEPS) {
Log-Output "Skip: C:\$Filename already exists, not overwriting" Log-Output "Skip: C:\$Filename already exists, not overwriting"
@ -81,7 +82,7 @@ try {
# Don't use FetchAndImport-ModuleFromMetadata for common.psm1 - the common # Don't use FetchAndImport-ModuleFromMetadata for common.psm1 - the common
# module includes variables and functions that any other function may depend # module includes variables and functions that any other function may depend
# on. # on.
$module = Get-InstanceMetadataValue 'common-psm1' $module = Get-InstanceMetadataAttribute 'common-psm1'
New-Item -ItemType file -Force C:\common.psm1 | Out-Null New-Item -ItemType file -Force C:\common.psm1 | Out-Null
Set-Content C:\common.psm1 $module Set-Content C:\common.psm1 $module
Import-Module -Force C:\common.psm1 Import-Module -Force C:\common.psm1

View File

@ -135,7 +135,7 @@ function Add_GceMetadataServerRoute {
function Fetch-KubeEnv { function Fetch-KubeEnv {
# Testing / debugging: # Testing / debugging:
# First: # First:
# ${kube_env} = Get-InstanceMetadataValue 'kube-env' # ${kube_env} = Get-InstanceMetadataAttribute 'kube-env'
# or: # or:
# ${kube_env} = [IO.File]::ReadAllText(".\kubeEnv.txt") # ${kube_env} = [IO.File]::ReadAllText(".\kubeEnv.txt")
# ${kube_env_table} = ConvertFrom-Yaml ${kube_env} # ${kube_env_table} = ConvertFrom-Yaml ${kube_env}
@ -143,7 +143,7 @@ function Fetch-KubeEnv {
# ${kube_env_table}.GetType() # ${kube_env_table}.GetType()
# The type of kube_env is a powershell String. # The type of kube_env is a powershell String.
$kube_env = Get-InstanceMetadataValue 'kube-env' $kube_env = Get-InstanceMetadataAttribute 'kube-env'
$kube_env_table = ConvertFrom-Yaml ${kube_env} $kube_env_table = ConvertFrom-Yaml ${kube_env}
return ${kube_env_table} return ${kube_env_table}
} }
@ -882,7 +882,7 @@ function Configure-Kubelet {
# The Kubelet config is built by build-kubelet-config() in # The Kubelet config is built by build-kubelet-config() in
# cluster/gce/util.sh, and stored in the metadata server under the # cluster/gce/util.sh, and stored in the metadata server under the
# 'kubelet-config' key. # 'kubelet-config' key.
$kubelet_config = Get-InstanceMetadataValue 'kubelet-config' $kubelet_config = Get-InstanceMetadataAttribute 'kubelet-config'
Set-Content ${env:KUBELET_CONFIG} $kubelet_config Set-Content ${env:KUBELET_CONFIG} $kubelet_config
Log-Output "Kubelet config:`n$(Get-Content -Raw ${env:KUBELET_CONFIG})" Log-Output "Kubelet config:`n$(Get-Content -Raw ${env:KUBELET_CONFIG})"
} }

View File

@ -106,7 +106,7 @@ function Setup_WriteSshKeysScript {
# Fetch helper module for manipulating Windows user profiles. # Fetch helper module for manipulating Windows user profiles.
if (ShouldWrite-File $USER_PROFILE_MODULE) { if (ShouldWrite-File $USER_PROFILE_MODULE) {
$module = Get-InstanceMetadataValue 'user-profile-psm1' $module = Get-InstanceMetadataAttribute 'user-profile-psm1'
New-Item -ItemType file -Force $USER_PROFILE_MODULE | Out-Null New-Item -ItemType file -Force $USER_PROFILE_MODULE | Out-Null
Set-Content $USER_PROFILE_MODULE $module Set-Content $USER_PROFILE_MODULE $module
} }