Public/dsl.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
function New-ServiceDefinition {
    [CmdletBinding()]
    [Alias('Service')]
    param (

        [Parameter(Mandatory, Position=0)]
        [scriptblock]
        $ServiceSpec,

        [Parameter()]
        [switch]
        $DryRun,

        [Parameter()]
        [switch]
        $OutYaml
    )


    $serviceParam = @{}
    $serviceFunctions = [System.Collections.Generic.Dictionary[string,System.Management.Automation.ScriptBlock]]::new()

    function Name {
        param (
            [string]$Value
        )
        $serviceParam.Add('Name', $Value)
    }

    function NameSpace {
        param (
            [string]$Value
        )
        $serviceParam.Add('Namespace', $Value)
    }

    function Spec {
        param (
            [Parameter(Mandatory, Position=0)]
            [scriptblock]
            $ServicePort)


        $specParam = @{
            Selector = @{}
            PortObject = @()
        }

        $specFunctions = [System.Collections.Generic.Dictionary[string,System.Management.Automation.ScriptBlock]]::new()

        function Selector {
            param (
                [string]$Key,
                [string]$Value
            )

            $specParam["Selector"].Add($Key, $Value)
        }

        function Port {
            param (
                [scriptblock]$Properties
            )

            $portParams = @{}
            $portFunctions = [System.Collections.Generic.Dictionary[string,System.Management.Automation.ScriptBlock]]::new()

            function Number {
                param (
                    [int]$Value
                )
                $portParams['Port'] = $Value

            }

            function TargetPort {
                param (
                    [int]$Value
                )
                $portParams['TargetPort'] = $Value
            }

            function Name {
                param (
                    [string]$Value
                )
                $portParams['Name'] = $Value
            }

            $portFunctions.Add('Number', (Get-Item function:\Number).ScriptBlock)
            $portFunctions.Add('TargetPort', (Get-Item function:\TargetPort).ScriptBlock)
            $portFunctions.Add('Name', (Get-Item function:\Name).ScriptBlock)

            $Properties.InvokeWithContext($portFunctions, $null, $null)

            $p = New-k8sServicePort @portParams
            $specParam["PortObject"] += $p
        }

        $specFunctions.Add('Selector', (Get-Item function:\Selector).ScriptBlock)
        $specFunctions.Add('Port', (Get-Item function:\Port).ScriptBlock)

        $ServicePort.InvokeWithContext($specFunctions, $null, $null)
        $spec = New-k8sServiceSpec @specParam

        $serviceParam["Spec"] = $spec

    }


    $serviceFunctions.Add('Name', (Get-Item function:\Name).ScriptBlock)
    $serviceFunctions.Add('Namespace', (Get-Item function:\Namespace).ScriptBlock)
    $serviceFunctions.Add('Spec', (Get-Item function:\Spec).ScriptBlock)

    $ServiceSpec.InvokeWithContext($serviceFunctions, $null, $null)

    #& $ServiceSpec

    if ($null -eq $serviceParam["Name"]) {
        throw "Name is empty. You must provide a Serivce Name."
    }

    if ($null -eq $serviceParam["Spec"]) {
        throw "Spec is empty. You must define a Serivce Spec."
    }

    if ($DryRun) {
        $serviceParam.Add('DryRun', 'All')
    }

    $service = New-k8sService @serviceParam -OutYaml:$OutYaml

    Write-Output $service

}
<#
## Example 1


$service = K8sService -DryRun {
    Name testservice2
    Namespace testnamespace
    Spec {
        Selector service testservice
        Port {
            Number 80
            TargetPort 8080
            Name http
        }
        Port {
            Number 443
            TargetPort 8443
        }
    }
}

## Example 2


K8sService {
    Name testservice
    Namespace testnamespace
    Spec {
        Selector service testservice
        foreach ($item in @(80,8080))
        {
            Port {
                Number $item
            }
        }
    }
}

#>