Skip to content

Commit

Permalink
Merge pull request #93 from kris-nova/grpc-and-others
Browse files Browse the repository at this point in the history
Grpc and others
  • Loading branch information
krisnova authored Nov 20, 2021
2 parents e155a3f + eaac474 commit 9bbab02
Show file tree
Hide file tree
Showing 160 changed files with 29,297 additions and 714 deletions.
66 changes: 7 additions & 59 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,24 +420,6 @@ func RunCommandLineWithOptions() error {
},
},
},

// ********************************************************
// [ RPC ]
// ********************************************************

{
Name: "rpc",
Aliases: []string{"r"},
Usage: "Run in RPC mode. Only run this if you know what you are doing",
UsageText: "naml rpc",
Action: func(c *cli.Context) error {
err := RunRPC()
if err != nil {
return fmt.Errorf("unable to run in runtime mode: %v", err)
}
return nil
},
},
},
}
return app.Run(os.Args)
Expand Down Expand Up @@ -504,41 +486,11 @@ func AllInit(kubeConfigPath string, verbose bool, with []string) error {
}
logger.Debug("Kubeconfig Value: %s", kubeConfigPathValue)

// [ Child Runtime System ]
if len(with) > 0 {
for _, childPath := range with {
for i := 0; i < 3; i++ {
err := AddRPC(childPath)
if err != nil {
logger.Warning("Unable to add child naml %s: %v", childPath, err)
time.Sleep(time.Millisecond * 20)
} else {
break
}
}

}
}

// If running naml with children, register them with the registry
if len(remotes) > 0 {
err := RegisterRemoteApplications()
if err != nil {
return fmt.Errorf("unable to register children: %v", err)
}
}
return nil
}

// Install is used to install an application in Kubernetes
func Install(app Deployable) error {

// Check if app is a remote app
if remoteApp, ok := app.(*RPCApplication); ok {
// Do NOT pass in this local kubernetes client!
return remoteApp.Install(nil)
}

// Only grab a client if we are running in this instance!
client, err := Client()
if err != nil {
Expand All @@ -565,23 +517,19 @@ func List() {
fmt.Println("")
for _, app := range Registry() {
fmt.Printf("[%s]\n", app.Meta().Name)
fmt.Printf(" description : %s\n", app.Description())
fmt.Printf(" version : %s\n", app.Meta().ResourceVersion)
fmt.Printf(" Description : %s\n", app.Meta().Description)
fmt.Printf(" Version : %s\n", app.Meta().ResourceVersion)
app.Install(nil)
for _, obj := range app.Objects() {
_, kind := obj.GetObjectKind().GroupVersionKind().ToAPIVersionAndKind()
fmt.Printf(" > %s\n", kind)
}
fmt.Printf("\n")
}
}

// Uninstall is used to uninstall an application in Kubernetes
func Uninstall(app Deployable) error {

// Check if app is a remote app
if remoteApp, ok := app.(*RPCApplication); ok {
// Do NOT pass in this local kubernetes client!
return remoteApp.Uninstall(nil)
}

// Only grab a client if we are running in this instance!

client, err := Client()
if err != nil {
return err
Expand Down
19 changes: 19 additions & 0 deletions codify/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Codify

This is the `naml` feature that will convert YAML to Go.

### Adding an implementation

Each implementation will require special attention to detail.

The boilerplate is straightforward however there are some special considerations.

This is the `alias` method that will handle the weird Kubernetes import alias mechanism.

```
alias(generated, default string)
```

The main problem is that all objects will render as `v1` instead of their corresponding alias such as `corev1` or `metav1`.
This method will do it's best to figure these out (without using reflection).
There are members in the top of `codify.go` that call out the well-known subobjects that cannot be defaulted.
2 changes: 1 addition & 1 deletion codify/clusterrole.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (k ClusterRole) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}ClusterRole := %s
a.objects = append(a.objects, {{ .GoName }}ClusterRole)
x.objects = append(x.objects, {{ .GoName }}ClusterRole)
if client != nil {
_, err = client.RbacV1().ClusterRoles().Create(context.TODO(), {{ .GoName }}ClusterRole, v1.CreateOptions{})
Expand Down
2 changes: 1 addition & 1 deletion codify/clusterrolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (k ClusterRoleBinding) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}ClusterRoleBinding := %s
a.objects = append(a.objects, {{ .GoName }}ClusterRoleBinding)
x.objects = append(x.objects, {{ .GoName }}ClusterRoleBinding)
if client != nil {
_, err = client.RbacV1().ClusterRoleBindings().Create(context.TODO(), {{ .GoName }}ClusterRoleBinding, v1.CreateOptions{})
Expand Down
93 changes: 93 additions & 0 deletions codify/codify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// Copyright © 2021 Kris Nóva <[email protected]>
//
// 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 codify

import (
"strings"
"testing"
)

// TestAliasSubstitutionCorrectDefault is the simplest test.
// This will check that a 1 to 1 substitution occurs, while
// we are using the correct default.
func TestAliasSubstitutionCorrectDefault(t *testing.T) {

generated := "v1.Volume" // We know Volume is a corev1.Volume
result := alias(generated, "corev1")
if !strings.Contains(result, "corev1.Volume") {
t.Errorf("missing expected corev1.Volume: %s", result)
}
generated = "SomethingSomething v1.Volume SomethingSomething"
result = alias(generated, "corev1")
if !strings.Contains(result, "corev1.Volume") {
t.Errorf("missing expected corev1.Volume: %s", result)
}

generated = "v1.SadVolume" // We know SadVolume is unknown, but we expect a default anyway
result = alias(generated, "corev1")
if !strings.Contains(result, "corev1.SadVolume") {
t.Errorf("unexpected substitution: %s", result)
}

generated = "" // Check empty string
result = alias(generated, "corev1")
if strings.Contains(result, "corev1") {
t.Errorf("unexpected substitution: %s", result)
}
}

// TestAliasEdgeCases will exercise some (but not all) cases
// where the host object has "sub objects" of different types.
func TestAliasEdgeCases(t *testing.T) {

// We can assume
// v1.Volume should be corev1.Volume
// v1.ObjectMeta should be metav1.ObjectMeta

generated := "Something v1.Volume Else v1.ObjectMeta Other v1.ThisShouldDefault"
result := alias(generated, "appsv1")
// here we build the EXACT string we are expecting!
expected := "Something corev1.Volume Else metav1.ObjectMeta Other appsv1.ThisShouldDefault"
if result != expected {
t.Errorf("unexpected result")
t.Errorf("expected: %s", expected)
t.Errorf("result: %s", result)
}
}

// TestPolicyV1Beta1 will check for the special type "policyv1beta1"
//
// This is unique to RBAC, and is slightly different than the other
// alias checks within the Codify function.
//
// PolicyV1Interface
// policyv1beta1
//
func TestPolicyV1Beta1(t *testing.T) {
generated := "v1.PolicyV1Interface"
result := alias(generated, "appsv1")
if !strings.Contains(result, "policyv1beta1.PolicyV1Interface") {
t.Errorf("unexpected result: %s", result)
}
}
2 changes: 1 addition & 1 deletion codify/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (k ConfigMap) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}ConfigMap := %s
a.objects = append(a.objects, {{ .GoName }}ConfigMap)
x.objects = append(x.objects, {{ .GoName }}ConfigMap)
if client != nil {
_, err = client.CoreV1().ConfigMaps("{{ .KubeObject.Namespace }}").Create(context.TODO(), {{ .GoName }}ConfigMap, v1.CreateOptions{})
Expand Down
2 changes: 1 addition & 1 deletion codify/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (k CronJob) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}CronJob := %s
a.objects = append(a.objects, {{ .GoName }}CronJob)
x.objects = append(x.objects, {{ .GoName }}CronJob)
if client != nil {
_, err = client.BatchV1().CronJobs("{{ .KubeObject.Namespace }}").Create(context.TODO(), {{ .GoName }}CronJob, v1.CreateOptions{})
Expand Down
2 changes: 1 addition & 1 deletion codify/customresourcedefinition.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (k CustomResourceDefinition) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}CustomResourceDefinition := %s
a.objects = append(a.objects, {{ .GoName }}CustomResourceDefinition)
x.objects = append(x.objects, {{ .GoName }}CustomResourceDefinition)
if client != nil {
result := client.ExtensionsV1beta1().RESTClient().Post().Namespace("{{ .KubeObject.Namespace }}").Body({{ .GoName }}CustomResourceDefinition).Do(context.TODO())
Expand Down
2 changes: 1 addition & 1 deletion codify/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (k DaemonSet) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}DaemonSet := %s
a.objects = append(a.objects, {{ .GoName }}DaemonSet)
x.objects = append(x.objects, {{ .GoName }}DaemonSet)
if client != nil {
_, err = client.AppsV1().DaemonSets("{{ .KubeObject.Namespace }}").Create(context.TODO(), {{ .GoName }}DaemonSet, v1.CreateOptions{})
Expand Down
2 changes: 1 addition & 1 deletion codify/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (k Deployment) Install() (string, []string) {
install := fmt.Sprintf(`
// Adding a deployment: "{{ .KubeObject.Name }}"
{{ .GoName }}Deployment := %s
a.objects = append(a.objects, {{ .GoName }}Deployment)
x.objects = append(x.objects, {{ .GoName }}Deployment)
if client != nil {
_, err = client.AppsV1().Deployments("{{ .KubeObject.Namespace }}").Create(context.TODO(), {{ .GoName }}Deployment, v1.CreateOptions{})
Expand Down
2 changes: 1 addition & 1 deletion codify/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (k Ingress) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}Ingress := %s
a.objects = append(a.objects, {{ .GoName }}Ingress)
x.objects = append(x.objects, {{ .GoName }}Ingress)
if client != nil {
_, err = client.NetworkingV1().Ingresss("{{ .KubeObject.Namespace }}").Create(context.TODO(), {{ .GoName }}Ingress, v1.CreateOptions{})
Expand Down
2 changes: 1 addition & 1 deletion codify/ingressclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (k IngressClass) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}IngressClass := %s
a.objects = append(a.objects, {{ .GoName }}IngressClass)
x.objects = append(x.objects, {{ .GoName }}IngressClass)
if client != nil {
_, err = client.NetworkingV1().IngressClasses().Create(context.TODO(), {{ .GoName }}IngressClass, v1.CreateOptions{})
Expand Down
2 changes: 1 addition & 1 deletion codify/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (k Job) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}Job := %s
a.objects = append(a.objects, {{ .GoName }}Job)
x.objects = append(x.objects, {{ .GoName }}Job)
if client != nil {
_, err = client.BatchV1().Jobs("{{ .KubeObject.Namespace }}").Create(context.TODO(), {{ .GoName }}Job, v1.CreateOptions{})
Expand Down
2 changes: 1 addition & 1 deletion codify/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (k Namespace) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}Namespace := %s
a.objects = append(a.objects, {{ .GoName }}Namespace)
x.objects = append(x.objects, {{ .GoName }}Namespace)
if client != nil {
_, err = client.CoreV1().Namespaces().Create(context.TODO(), {{ .GoName }}Namespace, v1.CreateOptions{})
Expand Down
2 changes: 1 addition & 1 deletion codify/persistentvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (k PersistentVolume) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}PersistentVolume := %s
a.objects = append(a.objects, {{ .GoName }}PersistentVolume)
x.objects = append(x.objects, {{ .GoName }}PersistentVolume)
if client != nil {
_, err = client.CoreV1().PersistentVolumes("{{ .KubeObject.Namespace }}").Create(context.TODO(), {{ .GoName }}PersistentVolume, v1.CreateOptions{})
Expand Down
2 changes: 1 addition & 1 deletion codify/persistentvolumeclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (k PersistentVolumeClaim) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}PersistentVolumeClaim := %s
a.objects = append(a.objects, {{ .GoName }}PersistentVolumeClaim)
x.objects = append(x.objects, {{ .GoName }}PersistentVolumeClaim)
if client != nil {
_, err = client.CoreV1().PersistentVolumeClaims("{{ .KubeObject.Namespace }}").Create(context.TODO(), {{ .GoName }}PersistentVolumeClaim, v1.CreateOptions{})
Expand Down
2 changes: 1 addition & 1 deletion codify/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (k Pod) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}Pod := %s
a.objects = append(a.objects, {{ .GoName }}Pod)
x.objects = append(x.objects, {{ .GoName }}Pod)
if client != nil {
_, err = client.CoreV1().Pods("{{ .KubeObject.Namespace }}").Create(context.TODO(), {{ .GoName }}Pod, v1.CreateOptions{})
Expand Down
2 changes: 1 addition & 1 deletion codify/podsecuritypolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (k PodSecurityPolicy) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}PodSecurityPolicy := %s
a.objects = append(a.objects, {{ .GoName }}PodSecurityPolicy)
x.objects = append(x.objects, {{ .GoName }}PodSecurityPolicy)
if client != nil {
_, err = client.PolicyV1beta1().PodSecurityPolicies().Create(context.TODO(), {{ .GoName }}PodSecurityPolicy, v1.CreateOptions{})
Expand Down
2 changes: 1 addition & 1 deletion codify/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (k Role) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}Role := %s
a.objects = append(a.objects, {{ .GoName }}Role)
x.objects = append(x.objects, {{ .GoName }}Role)
if client != nil {
_, err = client.RbacV1().Roles("{{ .KubeObject.Namespace }}").Create(context.TODO(), {{ .GoName }}Role, v1.CreateOptions{})
Expand Down
2 changes: 1 addition & 1 deletion codify/rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (k RoleBinding) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}RoleBinding := %s
a.objects = append(a.objects, {{ .GoName }}RoleBinding)
x.objects = append(x.objects, {{ .GoName }}RoleBinding)
if client != nil {
_, err = client.RbacV1().RoleBindings("{{ .KubeObject.Namespace }}").Create(context.TODO(), {{ .GoName }}RoleBinding, v1.CreateOptions{})
Expand Down
2 changes: 1 addition & 1 deletion codify/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (k Secret) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}Secret := %s
a.objects = append(a.objects, {{ .GoName }}Secret)
x.objects = append(x.objects, {{ .GoName }}Secret)
if client != nil {
_, err = client.CoreV1().Secrets("{{ .KubeObject.Namespace }}").Create(context.TODO(), {{ .GoName }}Secret, v1.CreateOptions{})
Expand Down
2 changes: 1 addition & 1 deletion codify/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (k Service) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}Service := %s
a.objects = append(a.objects, {{ .GoName }}Service)
x.objects = append(x.objects, {{ .GoName }}Service)
if client != nil {
_, err = client.CoreV1().Services("{{ .KubeObject.Namespace }}").Create(context.TODO(), {{ .GoName }}Service, v1.CreateOptions{})
Expand Down
2 changes: 1 addition & 1 deletion codify/serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (k ServiceAccount) Install() (string, []string) {
l, packages := Literal(k.KubeObject)
install := fmt.Sprintf(`
{{ .GoName }}ServiceAccount := %s
a.objects = append(a.objects, {{ .GoName }}ServiceAccount)
x.objects = append(x.objects, {{ .GoName }}ServiceAccount)
if client != nil {
_, err = client.CoreV1().ServiceAccounts("{{ .KubeObject.Namespace }}").Create(context.TODO(), {{ .GoName }}ServiceAccount, v1.CreateOptions{})
Expand Down
Loading

0 comments on commit 9bbab02

Please sign in to comment.