Enhance metadata fetching functions.

Introduce Get-InstanceMetadata which can be used to fetch
non-"attribute" metadata values.
pull/564/head
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
# in the instance metadata returns $Default if set, otherwise returns $null.
function Get-InstanceMetadataValue {
function Get-InstanceMetadata {
param (
[parameter(Mandatory=$true)] [string]$Key,
[parameter(Mandatory=$false)] [string]$Default
)
$url = ("http://metadata.google.internal/computeMetadata/v1/instance/" +
"attributes/$Key")
$url = "http://metadata.google.internal/computeMetadata/v1/instance/$Key"
try {
$client = New-Object Net.WebClient
$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 {
param(
[parameter(Mandatory=$true)] [string]$Hash,

View File

@ -27,9 +27,10 @@ $ErrorActionPreference = 'Stop'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$ProgressPreference = 'SilentlyContinue'
# 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.
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
@ -63,7 +64,7 @@ function FetchAndImport-ModuleFromMetadata {
[parameter(Mandatory=$true)] [string]$Filename
)
$module = Get-InstanceMetadataValue $MetadataKey
$module = Get-InstanceMetadataAttribute $MetadataKey
if (Test-Path C:\$Filename) {
if (-not $REDO_STEPS) {
Log-Output "Skip: C:\$Filename already exists, not overwriting"
@ -81,7 +82,7 @@ try {
# Don't use FetchAndImport-ModuleFromMetadata for common.psm1 - the common
# module includes variables and functions that any other function may depend
# on.
$module = Get-InstanceMetadataValue 'common-psm1'
$module = Get-InstanceMetadataAttribute 'common-psm1'
New-Item -ItemType file -Force C:\common.psm1 | Out-Null
Set-Content C:\common.psm1 $module
Import-Module -Force C:\common.psm1

View File

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