Skip to content

Commit

Permalink
chore: Sensor related packages refactory (#1022)
Browse files Browse the repository at this point in the history
  • Loading branch information
whynowy authored Jan 19, 2021
1 parent 0abf123 commit d3673d8
Show file tree
Hide file tree
Showing 20 changed files with 77 additions and 126 deletions.
2 changes: 1 addition & 1 deletion store/configmap.go → sensors/artifacts/configmap.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package store
package artifacts

import (
"github.com/argoproj/argo-events/common"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package store
package artifacts

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion store/file.go → sensors/artifacts/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package store
package artifacts

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion store/file_test.go → sensors/artifacts/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package store
package artifacts

import (
"io/ioutil"
Expand Down
2 changes: 1 addition & 1 deletion store/git.go → sensors/artifacts/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package store
package artifacts

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion store/git_test.go → sensors/artifacts/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package store
package artifacts

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion store/inline.go → sensors/artifacts/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package store
package artifacts

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion store/inline_test.go → sensors/artifacts/inline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package store
package artifacts

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion store/resource.go → sensors/artifacts/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package store
package artifacts

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package store
package artifacts

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion store/s3.go → sensors/artifacts/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package store
package artifacts

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion store/s3_test.go → sensors/artifacts/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package store
package artifacts

import (
"testing"
Expand Down
30 changes: 29 additions & 1 deletion store/store.go → sensors/artifacts/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package store
package artifacts

import (
"fmt"
"strings"

"github.com/argoproj/argo-events/common"
"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

Expand All @@ -40,6 +43,31 @@ func FetchArtifact(reader ArtifactReader) (*unstructured.Unstructured, error) {
return decodeAndUnstructure(obj)
}

type Credentials struct {
accessKey string
secretKey string
}

// GetCredentials for this minio
func GetCredentials(art *v1alpha1.ArtifactLocation) (*Credentials, error) {
if art.S3 != nil {
accessKey, err := common.GetSecretFromVolume(art.S3.AccessKey)
if err != nil {
return nil, errors.Wrap(err, "failed to retrieve accessKey")
}
secretKey, err := common.GetSecretFromVolume(art.S3.SecretKey)
if err != nil {
return nil, errors.Wrap(err, "failed to retrieve secretKey")
}
return &Credentials{
accessKey: strings.TrimSpace(accessKey),
secretKey: strings.TrimSpace(secretKey),
}, nil
}

return nil, nil
}

// GetArtifactReader returns the ArtifactReader for this location
func GetArtifactReader(loc *v1alpha1.ArtifactLocation, creds *Credentials) (ArtifactReader, error) {
if loc.S3 != nil {
Expand Down
28 changes: 26 additions & 2 deletions store/store_test.go → sensors/artifacts/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package store
package artifacts

import (
"context"
"io/ioutil"
"testing"

"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
"github.com/stretchr/testify/assert"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
)

type FakeWorkflowArtifactReader struct{}
Expand All @@ -38,6 +42,26 @@ func TestFetchArtifact(t *testing.T) {
assert.Equal(t, "Workflow", obj.GetKind())
}

func TestGetCredentials(t *testing.T) {
fakeClient := fake.NewSimpleClientset()

mySecretCredentials := &apiv1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "testing",
},
Data: map[string][]byte{"access": []byte("token"), "secret": []byte("value")},
}
_, err := fakeClient.CoreV1().Secrets("testing").Create(context.TODO(), mySecretCredentials, metav1.CreateOptions{})
assert.Nil(t, err)

// creds should be nil for unknown minio type
unknownArtifact := &v1alpha1.ArtifactLocation{}
creds, err := GetCredentials(unknownArtifact)
assert.Nil(t, creds)
assert.Nil(t, err)
}

func TestGetArtifactReader(t *testing.T) {
// test unknown failure
location := &v1alpha1.ArtifactLocation{}
Expand All @@ -50,7 +74,7 @@ func TestGetArtifactReader(t *testing.T) {
}

func TestDecodeSensor(t *testing.T) {
b, err := ioutil.ReadFile("../examples/sensors/multi-trigger-sensor.yaml")
b, err := ioutil.ReadFile("../../examples/sensors/multi-trigger-sensor.yaml")
assert.Nil(t, err)
_, err = decodeAndUnstructure(b)
assert.Nil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion store/url.go → sensors/artifacts/url.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package store
package artifacts

import (
"crypto/tls"
Expand Down
2 changes: 1 addition & 1 deletion store/url_test.go → sensors/artifacts/url_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package store
package artifacts

import (
"fmt"
Expand Down
8 changes: 4 additions & 4 deletions sensors/triggers/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
"github.com/argoproj/argo-events/store"
"github.com/argoproj/argo-events/sensors/artifacts"
)

func FetchKubernetesResource(source *v1alpha1.ArtifactLocation) (*unstructured.Unstructured, error) {
if source == nil {
return nil, errors.Errorf("trigger source for k8s is empty")
}
creds, err := store.GetCredentials(source)
creds, err := artifacts.GetCredentials(source)
if err != nil {
return nil, err
}
reader, err := store.GetArtifactReader(source, creds)
reader, err := artifacts.GetArtifactReader(source, creds)
if err != nil {
return nil, err
}
uObj, err := store.FetchArtifact(reader)
uObj, err := artifacts.FetchArtifact(reader)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions sensors/triggers/standard-k8s/standar-k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ import (
"k8s.io/client-go/kubernetes"

"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
"github.com/argoproj/argo-events/sensors/artifacts"
"github.com/argoproj/argo-events/sensors/policy"
"github.com/argoproj/argo-events/sensors/triggers"
"github.com/argoproj/argo-events/store"
)

// StandardK8STrigger implements Trigger interface for standard Kubernetes resources
Expand Down Expand Up @@ -74,11 +74,11 @@ func (k8sTrigger *StandardK8sTrigger) FetchResource(ctx context.Context) (interf
if trigger.Template.K8s.Source == nil {
return nil, errors.Errorf("trigger source for k8s is empty")
}
creds, err := store.GetCredentials(trigger.Template.K8s.Source)
creds, err := artifacts.GetCredentials(trigger.Template.K8s.Source)
if err != nil {
return nil, err
}
reader, err := store.GetArtifactReader(trigger.Template.K8s.Source, creds)
reader, err := artifacts.GetArtifactReader(trigger.Template.K8s.Source, creds)
if err != nil {
return nil, err
}
Expand All @@ -87,7 +87,7 @@ func (k8sTrigger *StandardK8sTrigger) FetchResource(ctx context.Context) (interf

// uObj will either hold the resource definition stored in the trigger or just
// a stub to provide enough information to fetch the object from K8s cluster
uObj, err := store.FetchArtifact(reader)
uObj, err := artifacts.FetchArtifact(reader)
if err != nil {
return nil, err
}
Expand Down
52 changes: 0 additions & 52 deletions store/creds.go

This file was deleted.

49 changes: 0 additions & 49 deletions store/creds_test.go

This file was deleted.

0 comments on commit d3673d8

Please sign in to comment.