Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Move ResourceID from root to its own package
Browse files Browse the repository at this point in the history
- run go fmt on all files
  • Loading branch information
stefanprodan committed Jun 27, 2019
1 parent 531f409 commit 6e9bfc5
Show file tree
Hide file tree
Showing 69 changed files with 354 additions and 356 deletions.
4 changes: 2 additions & 2 deletions api/v11/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ package v11
import (
"context"

"github.com/weaveworks/flux"
"github.com/weaveworks/flux/api/v10"
"github.com/weaveworks/flux/api/v6"
"github.com/weaveworks/flux/resourceid"
)

type ListServicesOptions struct {
Namespace string
Services []flux.ResourceID
Services []resourceid.ResourceID
}

type Server interface {
Expand Down
8 changes: 4 additions & 4 deletions api/v6/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package v6
import (
"context"

"github.com/weaveworks/flux"
"github.com/weaveworks/flux/cluster"
"github.com/weaveworks/flux/git"
"github.com/weaveworks/flux/job"
"github.com/weaveworks/flux/resourceid"
"github.com/weaveworks/flux/ssh"
"github.com/weaveworks/flux/update"
)

type ImageStatus struct {
ID flux.ResourceID
ID resourceid.ResourceID
Containers []Container
}

Expand All @@ -31,13 +31,13 @@ const (
)

type ControllerStatus struct {
ID flux.ResourceID
ID resourceid.ResourceID
Containers []Container
ReadOnly ReadOnlyReason
Status string
Rollout cluster.RolloutStatus
SyncError string
Antecedent flux.ResourceID
Antecedent resourceid.ResourceID
Labels map[string]string
Automated bool
Locked bool
Expand Down
10 changes: 5 additions & 5 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"errors"

"github.com/weaveworks/flux"
"github.com/weaveworks/flux/policy"
"github.com/weaveworks/flux/resource"
"github.com/weaveworks/flux/resourceid"
"github.com/weaveworks/flux/ssh"
)

Expand All @@ -27,8 +27,8 @@ const (
type Cluster interface {
// Get all of the services (optionally, from a specific namespace), excluding those
AllWorkloads(ctx context.Context, maybeNamespace string) ([]Workload, error)
SomeWorkloads(ctx context.Context, ids []flux.ResourceID) ([]Workload, error)
IsAllowedResource(flux.ResourceID) bool
SomeWorkloads(ctx context.Context, ids []resourceid.ResourceID) ([]Workload, error)
IsAllowedResource(resourceid.ResourceID) bool
Ping() error
Export(ctx context.Context) ([]byte, error)
Sync(SyncSet) error
Expand Down Expand Up @@ -60,7 +60,7 @@ type RolloutStatus struct {

// Workload describes a cluster resource that declares versioned images.
type Workload struct {
ID flux.ResourceID
ID resourceid.ResourceID
Status string // A status summary for display
// Is the controller considered read-only because it's under the
// control of the platform. In the case of Kubernetes, we simply
Expand All @@ -70,7 +70,7 @@ type Workload struct {
// resource through some mechanism (like an operator, or custom
// resource controller), we try to record the ID of that resource
// in this field.
Antecedent flux.ResourceID
Antecedent resourceid.ResourceID
Labels map[string]string
Policies policy.Set
Rollout RolloutStatus
Expand Down
4 changes: 2 additions & 2 deletions cluster/kubernetes/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/weaveworks/flux"
"github.com/weaveworks/flux/image"
"github.com/weaveworks/flux/registry"
"github.com/weaveworks/flux/resourceid"
)

func mergeCredentials(log func(...interface{}) error,
Expand Down Expand Up @@ -145,7 +145,7 @@ func (c *Cluster) ImagesToFetch() registry.ImageCreds {

imageCreds := make(registry.ImageCreds)
for _, workload := range workloads {
logger := log.With(c.logger, "resource", flux.MakeResourceID(ns.Name, kind, workload.GetName()))
logger := log.With(c.logger, "resource", resourceid.MakeResourceID(ns.Name, kind, workload.GetName()))
mergeCredentials(logger.Log, c.includeImage, c.client, ns.Name, workload.podTemplate, imageCreds, seenCreds)
}

Expand Down
16 changes: 8 additions & 8 deletions cluster/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
k8sclientdynamic "k8s.io/client-go/dynamic"
k8sclient "k8s.io/client-go/kubernetes"

"github.com/weaveworks/flux"
"github.com/weaveworks/flux/cluster"
"github.com/weaveworks/flux/cluster/kubernetes/resource"
kresource "github.com/weaveworks/flux/cluster/kubernetes/resource"
fhrclient "github.com/weaveworks/flux/integrations/client/clientset/versioned"
"github.com/weaveworks/flux/resourceid"
"github.com/weaveworks/flux/ssh"
)

Expand Down Expand Up @@ -98,7 +98,7 @@ type Cluster struct {

// syncErrors keeps a record of all per-resource errors during
// the sync from Git repo to the cluster.
syncErrors map[flux.ResourceID]error
syncErrors map[resourceid.ResourceID]error
muSyncErrors sync.RWMutex

allowedNamespaces []string
Expand Down Expand Up @@ -128,7 +128,7 @@ func NewCluster(client ExtendedClient, applier Applier, sshKeyRing ssh.KeyRing,
// SomeWorkloads returns the workloads named, missing out any that don't
// exist in the cluster or aren't in an allowed namespace.
// They do not necessarily have to be returned in the order requested.
func (c *Cluster) SomeWorkloads(ctx context.Context, ids []flux.ResourceID) (res []cluster.Workload, err error) {
func (c *Cluster) SomeWorkloads(ctx context.Context, ids []resourceid.ResourceID) (res []cluster.Workload, err error) {
var workloads []cluster.Workload
for _, id := range ids {
if !c.IsAllowedResource(id) {
Expand Down Expand Up @@ -192,7 +192,7 @@ func (c *Cluster) AllWorkloads(ctx context.Context, namespace string) (res []clu

for _, workload := range workloads {
if !isAddon(workload) {
id := flux.MakeResourceID(ns.Name, kind, workload.GetName())
id := resourceid.MakeResourceID(ns.Name, kind, workload.GetName())
c.muSyncErrors.RLock()
workload.syncError = c.syncErrors[id]
c.muSyncErrors.RUnlock()
Expand All @@ -208,7 +208,7 @@ func (c *Cluster) AllWorkloads(ctx context.Context, namespace string) (res []clu
func (c *Cluster) setSyncErrors(errs cluster.SyncError) {
c.muSyncErrors.Lock()
defer c.muSyncErrors.Unlock()
c.syncErrors = make(map[flux.ResourceID]error)
c.syncErrors = make(map[resourceid.ResourceID]error)
for _, e := range errs {
c.syncErrors[e.ResourceID] = e.Error
}
Expand Down Expand Up @@ -317,7 +317,7 @@ func (c *Cluster) getAllowedAndExistingNamespaces(ctx context.Context) ([]apiv1.
return namespaces.Items, nil
}

func (c *Cluster) IsAllowedResource(id flux.ResourceID) bool {
func (c *Cluster) IsAllowedResource(id resourceid.ResourceID) bool {
if len(c.allowedNamespaces) == 0 {
// All resources are allowed when all namespaces are allowed
return true
Expand All @@ -326,7 +326,7 @@ func (c *Cluster) IsAllowedResource(id flux.ResourceID) bool {
namespace, kind, name := id.Components()
namespaceToCheck := namespace

if namespace == resource.ClusterScope {
if namespace == kresource.ClusterScope {
// All cluster-scoped resources (not namespaced) are allowed ...
if kind != "namespace" {
return true
Expand Down
4 changes: 2 additions & 2 deletions cluster/kubernetes/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/runtime/schema"

"github.com/weaveworks/flux"
kresource "github.com/weaveworks/flux/cluster/kubernetes/resource"
"github.com/weaveworks/flux/image"
"github.com/weaveworks/flux/resource"
"github.com/weaveworks/flux/resourceid"
)

// ResourceScopes maps resource definitions (GroupVersionKind) to whether they are namespaced or not
Expand Down Expand Up @@ -133,7 +133,7 @@ func (m *manifests) ParseManifest(def []byte, source string) (map[string]resourc
return result, nil
}

func (m *manifests) SetWorkloadContainerImage(def []byte, id flux.ResourceID, container string, image image.Ref) ([]byte, error) {
func (m *manifests) SetWorkloadContainerImage(def []byte, id resourceid.ResourceID, container string, image image.Ref) ([]byte, error) {
return updateWorkload(def, id, container, image)
}

Expand Down
20 changes: 10 additions & 10 deletions cluster/kubernetes/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ import (
"k8s.io/apimachinery/pkg/util/strategicpatch"
k8sscheme "k8s.io/client-go/kubernetes/scheme"

"github.com/weaveworks/flux"
"github.com/weaveworks/flux/cluster/kubernetes/resource"
kresource "github.com/weaveworks/flux/cluster/kubernetes/resource"
"github.com/weaveworks/flux/resourceid"
)

func createManifestPatch(originalManifests, modifiedManifests []byte, originalSource, modifiedSource string) ([]byte, error) {
originalResources, err := resource.ParseMultidoc(originalManifests, originalSource)
originalResources, err := kresource.ParseMultidoc(originalManifests, originalSource)
if err != nil {
fmt.Errorf("cannot parse %s: %s", originalSource, err)
}

modifiedResources, err := resource.ParseMultidoc(modifiedManifests, modifiedSource)
modifiedResources, err := kresource.ParseMultidoc(modifiedManifests, modifiedSource)
if err != nil {
fmt.Errorf("cannot parse %s: %s", modifiedSource, err)
}
Expand Down Expand Up @@ -61,12 +61,12 @@ func createManifestPatch(originalManifests, modifiedManifests []byte, originalSo
}

func applyManifestPatch(originalManifests, patchManifests []byte, originalSource, patchSource string) ([]byte, error) {
originalResources, err := resource.ParseMultidoc(originalManifests, originalSource)
originalResources, err := kresource.ParseMultidoc(originalManifests, originalSource)
if err != nil {
return nil, fmt.Errorf("cannot parse %s: %s", originalSource, err)
}

patchResources, err := resource.ParseMultidoc(patchManifests, patchSource)
patchResources, err := kresource.ParseMultidoc(patchManifests, patchSource)
if err != nil {
return nil, fmt.Errorf("cannot parse %s: %s", patchSource, err)
}
Expand Down Expand Up @@ -120,7 +120,7 @@ func getFullScheme() *runtime.Scheme {
return fullScheme
}

func getPatch(originalManifest resource.KubeManifest, modifiedManifest resource.KubeManifest, scheme *runtime.Scheme) ([]byte, error) {
func getPatch(originalManifest kresource.KubeManifest, modifiedManifest kresource.KubeManifest, scheme *runtime.Scheme) ([]byte, error) {
groupVersion, err := schema.ParseGroupVersion(originalManifest.GroupVersion())
if err != nil {
return nil, fmt.Errorf("cannot parse groupVersion %q: %s", originalManifest.GroupVersion(), err)
Expand Down Expand Up @@ -192,7 +192,7 @@ func addIdentifyingData(apiVersion string, kind string, name string, namespace s
return obj, err
}

func applyPatch(originalManifest, patchManifest resource.KubeManifest, scheme *runtime.Scheme) ([]byte, error) {
func applyPatch(originalManifest, patchManifest kresource.KubeManifest, scheme *runtime.Scheme) ([]byte, error) {
groupVersion, err := schema.ParseGroupVersion(originalManifest.GroupVersion())
if err != nil {
return nil, fmt.Errorf("cannot parse groupVersion %q: %s", originalManifest.GroupVersion(), err)
Expand Down Expand Up @@ -229,6 +229,6 @@ func applyPatch(originalManifest, patchManifest resource.KubeManifest, scheme *r

// resourceID works like Resource.ResourceID() but avoids <cluster> namespaces,
// since they may be incorrect
func resourceID(manifest resource.KubeManifest) flux.ResourceID {
return flux.MakeResourceID(manifest.GetNamespace(), manifest.GetKind(), manifest.GetKind())
func resourceID(manifest kresource.KubeManifest) resourceid.ResourceID {
return resourceid.MakeResourceID(manifest.GetNamespace(), manifest.GetKind(), manifest.GetKind())
}
4 changes: 2 additions & 2 deletions cluster/kubernetes/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (

"github.com/pkg/errors"

"github.com/weaveworks/flux"
kresource "github.com/weaveworks/flux/cluster/kubernetes/resource"
"github.com/weaveworks/flux/policy"
"github.com/weaveworks/flux/resource"
"github.com/weaveworks/flux/resourceid"
)

func (m *manifests) UpdateWorkloadPolicies(def []byte, id flux.ResourceID, update policy.Update) ([]byte, error) {
func (m *manifests) UpdateWorkloadPolicies(def []byte, id resourceid.ResourceID, update policy.Update) ([]byte, error) {
resources, err := m.ParseManifest(def, "stdin")
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions cluster/kubernetes/policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/go-kit/kit/log"
"github.com/stretchr/testify/assert"

"github.com/weaveworks/flux"
"github.com/weaveworks/flux/policy"
"github.com/weaveworks/flux/resourceid"
)

func TestUpdatePolicies(t *testing.T) {
Expand Down Expand Up @@ -180,7 +180,7 @@ func TestUpdatePolicies(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
caseIn := templToString(t, annotationsTemplate, c.in)
caseOut := templToString(t, annotationsTemplate, c.out)
resourceID := flux.MustParseResourceID("default:deployment/nginx")
resourceID := resourceid.MustParseResourceID("default:deployment/nginx")
manifests := NewManifests(ConstNamespacer("default"), log.NewLogfmtLogger(os.Stdout))
out, err := manifests.UpdateWorkloadPolicies([]byte(caseIn), resourceID, c.update)
assert.Equal(t, c.wantErr, err != nil, "unexpected error value: %s", err)
Expand All @@ -192,7 +192,7 @@ func TestUpdatePolicies(t *testing.T) {
}

func TestUpdatePolicies_invalidTagPattern(t *testing.T) {
resourceID := flux.MustParseResourceID("default:deployment/nginx")
resourceID := resourceid.MustParseResourceID("default:deployment/nginx")
update := policy.Update{
Add: policy.Set{policy.TagPrefix("nginx"): "semver:invalid"},
}
Expand Down
14 changes: 7 additions & 7 deletions cluster/kubernetes/resource/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

"github.com/stretchr/testify/assert"

"github.com/weaveworks/flux"
"github.com/weaveworks/flux/cluster/kubernetes/testfiles"
"github.com/weaveworks/flux/resource"
"github.com/weaveworks/flux/resourceid"
)

// for convenience
Expand Down Expand Up @@ -211,9 +211,9 @@ items:
if len(list.Items) != 2 {
t.Fatalf("expected two items, got %+v", list.Items)
}
for i, id := range []flux.ResourceID{
flux.MustParseResourceID("ns:deployment/foo"),
flux.MustParseResourceID("ns:service/bar")} {
for i, id := range []resourceid.ResourceID{
resourceid.MustParseResourceID("ns:deployment/foo"),
resourceid.MustParseResourceID("ns:service/bar")} {
if list.Items[i].ResourceID() != id {
t.Errorf("At %d, expected %q, got %q", i, id, list.Items[i].ResourceID())
}
Expand Down Expand Up @@ -246,9 +246,9 @@ items:
if len(list.Items) != 2 {
t.Fatalf("expected two items, got %+v", list.Items)
}
for i, id := range []flux.ResourceID{
flux.MustParseResourceID("ns:deployment/foo"),
flux.MustParseResourceID("ns:deployment/bar")} {
for i, id := range []resourceid.ResourceID{
resourceid.MustParseResourceID("ns:deployment/foo"),
resourceid.MustParseResourceID("ns:deployment/bar")} {
if list.Items[i].ResourceID() != id {
t.Errorf("At %d, expected %q, got %q", i, id, list.Items[i].ResourceID())
}
Expand Down
8 changes: 4 additions & 4 deletions cluster/kubernetes/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package resource
import (
"strings"

yaml "gopkg.in/yaml.v2"
"gopkg.in/yaml.v2"

"github.com/weaveworks/flux"
fluxerr "github.com/weaveworks/flux/errors"
"github.com/weaveworks/flux/policy"
"github.com/weaveworks/flux/resource"
"github.com/weaveworks/flux/resourceid"
)

const (
Expand Down Expand Up @@ -65,12 +65,12 @@ func (o baseObject) GetName() string {
return o.Meta.Name
}

func (o baseObject) ResourceID() flux.ResourceID {
func (o baseObject) ResourceID() resourceid.ResourceID {
ns := o.Meta.Namespace
if ns == "" {
ns = ClusterScope
}
return flux.MakeResourceID(ns, o.Kind, o.Meta.Name)
return resourceid.MakeResourceID(ns, o.Kind, o.Meta.Name)
}

// SetNamespace implements KubeManifest.SetNamespace, so things with
Expand Down
Loading

0 comments on commit 6e9bfc5

Please sign in to comment.