-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove other commits, and add test files missing
Signed-off-by: Filinto Duran <[email protected]>
- Loading branch information
Showing
8 changed files
with
245 additions
and
7 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
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,75 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime/serializer/yaml" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/dapr/cli/pkg/print" | ||
) | ||
|
||
func getResources(resourcesFolder string) ([]client.Object, error) { | ||
// Create YAML decoder | ||
decUnstructured := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme) | ||
|
||
// Read files from the resources folder | ||
files, err := os.ReadDir(resourcesFolder) | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading resources folder: %w", err) | ||
} | ||
|
||
var resources []client.Object | ||
for _, file := range files { | ||
if file.IsDir() || (!strings.HasSuffix(file.Name(), ".yaml") && !strings.HasSuffix(file.Name(), ".json")) { | ||
continue | ||
} | ||
|
||
// Read file content | ||
content, err := os.ReadFile(filepath.Join(resourcesFolder, file.Name())) | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading file %s: %w", file.Name(), err) | ||
} | ||
|
||
// Decode YAML/JSON to unstructured | ||
obj := &unstructured.Unstructured{} | ||
_, _, err = decUnstructured.Decode(content, nil, obj) | ||
if err != nil { | ||
return nil, fmt.Errorf("error decoding file %s: %w", file.Name(), err) | ||
} | ||
|
||
resources = append(resources, obj) | ||
} | ||
|
||
return resources, nil | ||
} | ||
|
||
func createOrUpdateResources(ctx context.Context, cl client.Client, resources []client.Object, namespace string) error { | ||
// create resources in k8s | ||
for _, resource := range resources { | ||
// clone the resource to avoid modifying the original | ||
obj := resource.DeepCopyObject().(*unstructured.Unstructured) | ||
// Set namespace on the resource metadata | ||
obj.SetNamespace(namespace) | ||
|
||
print.InfoStatusEvent(os.Stdout, "Deploying resource %q kind %q to Kubernetes", obj.GetName(), obj.GetKind()) | ||
|
||
if err := cl.Create(ctx, obj); err != nil { | ||
if k8serrors.IsAlreadyExists(err) { | ||
print.InfoStatusEvent(os.Stdout, "Resource %q kind %q already exists, updating", obj.GetName(), obj.GetKind()) | ||
if err := cl.Update(ctx, obj); err != nil { | ||
return err | ||
} | ||
} else { | ||
return fmt.Errorf("error deploying resource %q kind %q to Kubernetes: %w", obj.GetName(), obj.GetKind(), err) | ||
} | ||
} | ||
} | ||
return 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,50 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetResources(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
folder string | ||
expectError bool | ||
expectedCount int | ||
expectedResourceKinds []string | ||
}{ | ||
{ | ||
name: "resources from testdata", | ||
folder: filepath.Join("testdata", "resources"), | ||
expectError: false, | ||
expectedCount: 3, | ||
expectedResourceKinds: []string{"Component", "Configuration", "Resiliency"}, | ||
}, | ||
{ | ||
name: "non-existent folder", | ||
folder: filepath.Join("testdata", "non-existent"), | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
resources, err := getResources(tt.folder) | ||
if tt.expectError { | ||
assert.Error(t, err) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
assert.Len(t, resources, tt.expectedCount) | ||
foundKinds := []string{} | ||
for _, resource := range resources { | ||
foundKinds = append(foundKinds, resource.GetObjectKind().GroupVersionKind().Kind) | ||
} | ||
assert.ElementsMatch(t, tt.expectedResourceKinds, foundKinds) | ||
}) | ||
} | ||
} |
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,10 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Configuration | ||
metadata: | ||
name: daprConfig | ||
namespace: default | ||
spec: | ||
tracing: | ||
samplingRate: "1" | ||
zipkin: | ||
endpointAddress: "http://localhost:9411/api/v2/spans" |
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,26 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Resiliency | ||
metadata: | ||
name: myresiliency | ||
scopes: | ||
- checkout | ||
|
||
spec: | ||
policies: | ||
retries: | ||
retryForever: | ||
policy: constant | ||
duration: 5s | ||
maxRetries: -1 | ||
|
||
circuitBreakers: | ||
simpleCB: | ||
maxRequests: 1 | ||
timeout: 5s | ||
trip: consecutiveFailures >= 5 | ||
|
||
targets: | ||
apps: | ||
order-processor: | ||
retry: retryForever | ||
circuitBreaker: simpleCB |
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,15 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: statestore | ||
spec: | ||
type: state.redis | ||
version: v1 | ||
initTimeout: 1m | ||
metadata: | ||
- name: redisHost | ||
value: localhost:6379 | ||
- name: redisPassword | ||
value: "" | ||
- name: actorStateStore | ||
value: "true" |