Skip to content

Commit

Permalink
Add Helm support to Python SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
lblackstone committed Apr 30, 2019
1 parent 29d2b48 commit 06fdb23
Show file tree
Hide file tree
Showing 13 changed files with 1,029 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
### Improvements

- Put all resources in specified provider namespace (https://github.com/pulumi/pulumi-kubernetes/pull/538)
- Add Helm support to Python SDK (https://github.com/pulumi/pulumi-kubernetes/pull/544)

### Bug fixes

Expand Down
85 changes: 85 additions & 0 deletions cmd/pulumi-gen-kubernetes/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2016-2019, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
)

// CopyFile copies a single file from src to dst
// From https://blog.depado.eu/post/copy-files-and-directories-in-go
func CopyFile(src, dst string) error {
var err error
var srcfd *os.File
var dstfd *os.File
var srcinfo os.FileInfo

if srcfd, err = os.Open(src); err != nil {
return err
}
defer srcfd.Close()

if dstfd, err = os.Create(dst); err != nil {
return err
}
defer dstfd.Close()

if _, err = io.Copy(dstfd, srcfd); err != nil {
return err
}
if srcinfo, err = os.Stat(src); err != nil {
return err
}
return os.Chmod(dst, srcinfo.Mode())
}

// CopyDir copies a whole directory recursively
// From https://blog.depado.eu/post/copy-files-and-directories-in-go
func CopyDir(src string, dst string) error {
var err error
var fds []os.FileInfo
var srcinfo os.FileInfo

if srcinfo, err = os.Stat(src); err != nil {
return err
}

if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
return err
}

if fds, err = ioutil.ReadDir(src); err != nil {
return err
}
for _, fd := range fds {
srcfp := path.Join(src, fd.Name())
dstfp := path.Join(dst, fd.Name())

if fd.IsDir() {
if err = CopyDir(srcfp, dstfp); err != nil {
fmt.Println(err)
}
} else {
if err = CopyFile(srcfp, dstfp); err != nil {
fmt.Println(err)
}
}
}
return nil
}
41 changes: 24 additions & 17 deletions cmd/pulumi-gen-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io/ioutil"
"log"
"os"
"path/filepath"

"github.com/pulumi/pulumi-kubernetes/pkg/gen"
)
Expand Down Expand Up @@ -135,56 +136,62 @@ func writeNodeJSClient(data map[string]interface{}, outdir, templateDir string)
}

func writePythonClient(data map[string]interface{}, outdir, templateDir string) {
sdkDir := filepath.Join(outdir, "pulumi_kubernetes")

err := gen.PythonClient(data, templateDir,
func(initPy string) error {
return ioutil.WriteFile(
fmt.Sprintf("%s/pulumi_kubernetes/__init__.py", outdir), []byte(initPy), 0777)
return ioutil.WriteFile(filepath.Join(sdkDir, "__init__.py"), []byte(initPy), 0777)
},
func(group, initPy string) error {
path := fmt.Sprintf("%s/pulumi_kubernetes/%s", outdir, group)
destDir := filepath.Join(sdkDir, group)

err := os.MkdirAll(path, 0700)
err := os.MkdirAll(destDir, 0700)
if err != nil {
return err
}

return ioutil.WriteFile(fmt.Sprintf("%s/__init__.py", path), []byte(initPy), 0777)
return ioutil.WriteFile(filepath.Join(destDir, "__init__.py"), []byte(initPy), 0777)
},
func(crBytes string) error {
path := fmt.Sprintf("%s/pulumi_kubernetes/apiextensions", outdir)
destDir := filepath.Join(sdkDir, "apiextensions")

err := os.MkdirAll(path, 0700)
err := os.MkdirAll(destDir, 0700)
if err != nil {
return err
}

return ioutil.WriteFile(
fmt.Sprintf("%s/pulumi_kubernetes/apiextensions/CustomResource.py", outdir),
filepath.Join(destDir, "CustomResource.py"),
[]byte(crBytes), 0777)
},
func(group, version, initPy string) error {
path := fmt.Sprintf("%s/pulumi_kubernetes/%s/%s", outdir, group, version)
destDir := filepath.Join(sdkDir, group, version)

err := os.MkdirAll(path, 0700)
err := os.MkdirAll(destDir, 0700)
if err != nil {
return err
}

return ioutil.WriteFile(fmt.Sprintf("%s/__init__.py", path), []byte(initPy), 0777)
return ioutil.WriteFile(filepath.Join(destDir, "__init__.py"), []byte(initPy), 0777)
},
func(group, version, kind, kindPy string) error {
path := fmt.Sprintf("%s/pulumi_kubernetes/%s/%s/%s.py", outdir, group, version, kind)
return ioutil.WriteFile(path, []byte(kindPy), 0777)
destDir := filepath.Join(sdkDir, group, version, fmt.Sprintf("%s.py", kind))
return ioutil.WriteFile(destDir, []byte(kindPy), 0777)
},
func(casingPy string) error {
return ioutil.WriteFile(
fmt.Sprintf("%s/pulumi_kubernetes/tables.py", outdir), []byte(casingPy), 0777)
destDir := filepath.Join(sdkDir, "tables.py")
return ioutil.WriteFile(destDir, []byte(casingPy), 0777)
},
func(yamlPy string) error {
return ioutil.WriteFile(
fmt.Sprintf("%s/pulumi_kubernetes/yaml.py", outdir), []byte(yamlPy), 0777)
destDir := filepath.Join(sdkDir, "yaml.py")
return ioutil.WriteFile(destDir, []byte(yamlPy), 0777)
})
if err != nil {
panic(err)
}

err = CopyDir(filepath.Join(templateDir, "helm"), filepath.Join(sdkDir, "helm"))
if err != nil {
panic(err)
}
}
8 changes: 8 additions & 0 deletions pkg/gen/python-templates/helm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# coding=utf-8
# *** WARNING: this file was generated by the Pulumi Kubernetes codegen tool. ***
# *** Do not edit by hand unless you're certain you know what you are doing! ***

# Make subpackages available:
__all__ = [
"v2",
]
6 changes: 6 additions & 0 deletions pkg/gen/python-templates/helm/v2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# coding=utf-8
# *** WARNING: this file was generated by the Pulumi Kubernetes codegen tool. ***
# *** Do not edit by hand unless you're certain you know what you are doing! ***

# Export this package's modules as members:
from .helm import *
Loading

0 comments on commit 06fdb23

Please sign in to comment.