-
Notifications
You must be signed in to change notification settings - Fork 203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to create components in resources folder to kubernetes #1473
Open
filintod
wants to merge
4
commits into
dapr:master
Choose a base branch
from
filintod:filinto/add-component
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
245d50c
remove other commits, and add test files missing
filintod 247bb48
reinsert image policy files
filintod 064cbb4
limit number of configurations, and use correct name appconfig
filintod edc6e7d
Merge branch 'master' into filinto/add-component
mikeee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,91 @@ | ||
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" | ||
) | ||
|
||
// getResources returns a list of Kubernetes resources from the given resources folder. | ||
// There can be only one configuration file as dapr cli is not enabled to take a configuration per app and can only | ||
// use the configuration named `appconfig`. | ||
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) | ||
} | ||
|
||
// we can only have one configuration file as dapr cli is not enabled to take a configuration per app and can only | ||
// use the configuration named `appconfig`. | ||
var configurationAlreadyFound bool | ||
|
||
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) | ||
} | ||
|
||
// check if the resource is a configuration | ||
if obj.GetKind() == "Configuration" { | ||
if configurationAlreadyFound { | ||
return nil, fmt.Errorf("error: multiple configuration files found in %s. Only one configuration file is allowed", resourcesFolder) | ||
} | ||
configurationAlreadyFound = true | ||
obj.SetName("appconfig") | ||
} | ||
|
||
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" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this error is always nil at this point