Module: Biosphere::Kube

Included in:
TerraformProxy
Defined in:
lib/biosphere/kube.rb

Defined Under Namespace

Classes: Client

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_manifest_files(dir) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/biosphere/kube.rb', line 169

def self.find_manifest_files(dir)
    files = []
    files += Dir[dir + "/**/*.erb"]

    files += Dir[dir + "/**/*.yaml"]
    files
end

.kube_merge_resource_for_put!(current, new_version) ⇒ Object

Applies seleted properties from the current resource (as fetched from apiserver) to the new_version (as read from manifest file)



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/biosphere/kube.rb', line 224

def self.kube_merge_resource_for_put!(current, new_version)
    if current[:metadata]
        new_version[:metadata][:selfLink] = current[:metadata][:selfLink] if current[:metadata][:selfLink]
        new_version[:metadata][:uid] = current[:metadata][:uid] if current[:metadata][:uid]
        new_version[:metadata][:resourceVersion] = current[:metadata][:resourceVersion] if current[:metadata][:resourceVersion]
    end

    if current[:spec]
        new_version[:spec] = {} if !new_version[:spec]

        # handle spec.clusterIP
        if new_version[:spec][:clusterIP] && new_version[:spec][:clusterIP] != current[:spec][:clusterIP]
            raise ArgumentError, "Tried to modify spec.clusterIP from #{current[:spec][:clusterIP]} to #{new_version[:spec][:clusterIP]} but the field is immutable"
        end
        new_version[:spec][:clusterIP] = current[:spec][:clusterIP] if current[:spec][:clusterIP]

    end
    return new_version
end

.load_resources(file, context = {}) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/biosphere/kube.rb', line 177

def self.load_resources(file, context={})
    resources = []
    #puts "Loading file #{File.absolute_path(file)}"
    str = ERB.new(IO.read(file)).result(OpenStruct.new(context).instance_eval { binding })
    begin
        Psych.load_stream(str) do |document|
            kind = document["kind"]
            resource = ::Kubeclient::Resource.new(document)
            resources << resource
        end
    rescue Psych::SyntaxError => e
        STDERR.puts "\n"
        STDERR.puts "YAML syntax error while parsing file #{file}. Notice this happens after ERB templating, so line numbers might not match."
        STDERR.puts "Here are the relevant lines. Error '#{e.problem}' occured at line #{e.line}"
        STDERR.puts "Notice that yaml is very picky about indentation when you have arrays and maps. Check those first."
        lines = str.split("\n")
        start_line = [0, e.line - 3].max
        end_line = [lines.length - 1, e.line + 3].min
        lines[start_line..end_line].each_with_index do |line, num|
            num += start_line
            if num == e.line
                STDERR.printf("%04d>  %s\n".red, num, line)
            else
                STDERR.printf("%04d|  %s\n", num, line)
            end

        end
        STDERR.puts "\n"
        raise e
    end
    return resources
end

Instance Method Details

#kube_create_resource(client, resource) ⇒ Object



210
211
212
213
214
215
216
217
218
# File 'lib/biosphere/kube.rb', line 210

def kube_create_resource(client, resource)
    name = resource[:metadata][:name]
    resource_name = client.instance_variable_get("@entities")[resource[:kind].downcase].resource_name
    ns_prefix = client.build_namespace_prefix(resource[:metadata][:namespace])
    client.handle_exception do
        client.rest_client[ns_prefix + resource_name]
        .post(resource.to_h.to_json, { 'Content-Type' => 'application/json' }.merge(client.instance_variable_get("@headers")))
    end
end

#kube_get_client(hostname, ssl_options) ⇒ Object



165
166
167
# File 'lib/biosphere/kube.rb', line 165

def kube_get_client(hostname, ssl_options)
    return Client.new(hostname, ssl_options)
end

#kube_test(str) ⇒ Object

end of class Client



161
162
163
# File 'lib/biosphere/kube.rb', line 161

def kube_test(str)
    return str
end