-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
228 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
name: me | ||
pluto: not a planet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
not: | ||
a | ||
yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx-deployment | ||
labels: | ||
app: nginx | ||
spec: | ||
replicas: 3 | ||
selector: | ||
matchLabels: | ||
app: nginx | ||
template: | ||
metadata: | ||
labels: | ||
app: nginx | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx:1.14.2 | ||
ports: | ||
- containerPort: 80 | ||
--- | ||
apiVersion: v1 | ||
kind: PersistentVolume | ||
metadata: | ||
name: my-pv | ||
spec: | ||
capacity: | ||
storage: 1Gi | ||
accessModes: | ||
- ReadWriteOnce | ||
storageClassName: standard | ||
hostPath: | ||
path: /mnt/data | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: nginx-service | ||
spec: | ||
selector: | ||
app: nginx | ||
ports: | ||
- protocol: TCP | ||
port: 80 | ||
targetPort: 80 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package helpers | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"sigs.k8s.io/kustomize/kyaml/kio" | ||
) | ||
|
||
func ValidateKubernetesYamlFile(absPath string) error { | ||
if !filepath.IsAbs(absPath) { | ||
return fmt.Errorf("given path is not an absolute path %s", absPath) | ||
} | ||
b, err := os.ReadFile(absPath) | ||
if err != nil { | ||
return fmt.Errorf("failed reading file: %s, err: %w", absPath, err) | ||
} | ||
n, err := kio.FromBytes(b) | ||
if err != nil { | ||
return fmt.Errorf("failed parsing file as kubernetes manifests file: %s, err: %w", absPath, err) | ||
} | ||
|
||
for i := range n { | ||
obj := n[i] | ||
if obj.IsNilOrEmpty() { | ||
return fmt.Errorf("given file %s contains an invalid kubenretes manifest", absPath) | ||
} | ||
if obj.GetKind() == "" || obj.GetApiVersion() == "" { | ||
return fmt.Errorf("given file %s contains an invalid kubenretes manifest", absPath) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func GetAbsFilePaths(paths []string, isDir bool) ([]string, error) { | ||
out := make([]string, len(paths)) | ||
for i := range paths { | ||
path := paths[i] | ||
absPath, err := filepath.Abs(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to validate path %s : %w", path, err) | ||
} | ||
f, err := os.Stat(absPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to validate path %s : %w", absPath, err) | ||
} | ||
if isDir && !f.IsDir() { | ||
return nil, fmt.Errorf("given path is not a directory. %s", absPath) | ||
} | ||
if !isDir && !f.Mode().IsRegular() { | ||
return nil, fmt.Errorf("give path is not a file. %s", absPath) | ||
} | ||
|
||
out[i] = absPath | ||
} | ||
|
||
return out, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package helpers | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestValidateKubernetesYaml(t *testing.T) { | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
t.Fatalf("could not get current working directory") | ||
} | ||
|
||
cases := map[string]struct { | ||
expectErr bool | ||
inputPath string | ||
}{ | ||
//"invalidPath": {expectErr: true, inputPath: fmt.Sprintf("%s/invalid/path", cwd)}, | ||
//"notAbs": {expectErr: true, inputPath: fmt.Sprintf("invalid/path")}, | ||
//"valid": {expectErr: false, inputPath: fmt.Sprintf("%s/test-data/valid.yaml", cwd)}, | ||
//"notYaml": {expectErr: true, inputPath: fmt.Sprintf("%s/test-data/notyaml.yaml", cwd)}, | ||
"notk8s": {expectErr: true, inputPath: fmt.Sprintf("%s/test-data/notk8s.yaml", cwd)}, | ||
} | ||
|
||
for k := range cases { | ||
cErr := ValidateKubernetesYamlFile(cases[k].inputPath) | ||
if cases[k].expectErr && cErr == nil { | ||
t.Fatalf("%s expected error but did not receive error", k) | ||
} | ||
if !cases[k].expectErr && cErr != nil { | ||
t.Fatalf("%s did not expect error but received error", k) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,60 @@ | ||
package k8s | ||
|
||
import ( | ||
"embed" | ||
"os" | ||
|
||
"github.com/cnoe-io/idpbuilder/pkg/util" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func BuildCustomizedManifests(filePath, fsPath string, resourceFS embed.FS, scheme *runtime.Scheme, templateData any) ([][]byte, error) { | ||
rawResources, err := util.ConvertFSToBytes(resourceFS, fsPath, templateData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if filePath == "" { | ||
return rawResources, nil | ||
} | ||
|
||
bs, _, err := applyOverrides(filePath, rawResources, scheme, templateData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return bs, nil | ||
} | ||
|
||
func BuildCustomizedObjects(filePath, fsPath string, resourceFS embed.FS, scheme *runtime.Scheme, templateData any) ([]client.Object, error) { | ||
rawResources, err := util.ConvertFSToBytes(resourceFS, fsPath, templateData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if filePath == "" { | ||
return ConvertRawResourcesToObjects(scheme, rawResources) | ||
} | ||
|
||
_, objs, err := applyOverrides(filePath, rawResources, scheme, templateData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return objs, nil | ||
} | ||
|
||
func applyOverrides(filePath string, originalFiles [][]byte, scheme *runtime.Scheme, templateData any) ([][]byte, []client.Object, error) { | ||
customBS, err := os.ReadFile(filePath) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
rendered, err := util.ApplyTemplate(customBS, templateData) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return ConvertYamlToObjectsWithOverride(scheme, originalFiles, rendered) | ||
} |
Oops, something went wrong.