Skip to content
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

Split various packages to reduce dependency chain #10909

Merged
merged 3 commits into from Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion cilium/cmd/bpf_policy_get.go
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/cilium/cilium/pkg/byteorder"
"github.com/cilium/cilium/pkg/command"
"github.com/cilium/cilium/pkg/identity"
identitymodel "github.com/cilium/cilium/pkg/identity/model"
"github.com/cilium/cilium/pkg/maps/policymap"
"github.com/cilium/cilium/pkg/policy/trafficdirection"
"github.com/cilium/cilium/pkg/u8proto"
Expand Down Expand Up @@ -138,7 +139,7 @@ func formatMap(w io.Writer, statsMap []policymap.PolicyEntryDump) {
fmt.Fprintf(os.Stderr, "Was impossible to retrieve label ID %d: %s\n",
id, err)
} else {
labelsID[id] = identity.NewIdentityFromModel(lbls)
labelsID[id] = identitymodel.NewIdentityFromModel(lbls)
}
}

Expand Down
3 changes: 2 additions & 1 deletion daemon/cmd/daemon.go
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/identity/cache"
"github.com/cilium/cilium/pkg/identity/identitymanager"
identitymodel "github.com/cilium/cilium/pkg/identity/model"
"github.com/cilium/cilium/pkg/ipam"
"github.com/cilium/cilium/pkg/ipcache"
"github.com/cilium/cilium/pkg/k8s"
Expand Down Expand Up @@ -649,7 +650,7 @@ func (d *Daemon) GetIdentity(securityIdentity uint64) (*models.Identity, error)
if ident == nil {
return nil, fmt.Errorf("identity %d not found", securityIdentity)
}
return ident.GetModel(), nil
return identitymodel.CreateModel(ident), nil
}

// GetEndpointInfo returns endpoint info for a given IP address. Hubble uses this function to populate
Expand Down
3 changes: 2 additions & 1 deletion daemon/cmd/daemon_main.go
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/cilium/cilium/pkg/k8s/watchers"
"github.com/cilium/cilium/pkg/kvstore"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/labelsfilter"
"github.com/cilium/cilium/pkg/loadinfo"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
Expand Down Expand Up @@ -961,7 +962,7 @@ func initEnv(cmd *cobra.Command) {
if !option.Config.EnableIPv4 && !option.Config.EnableIPv6 {
log.Fatal("Either IPv4 or IPv6 addressing must be enabled")
}
if err := labels.ParseLabelPrefixCfg(option.Config.Labels, option.Config.LabelPrefixFile); err != nil {
if err := labelsfilter.ParseLabelPrefixCfg(option.Config.Labels, option.Config.LabelPrefixFile); err != nil {
log.WithError(err).Fatal("Unable to parse Label prefix configuration")
}

Expand Down
4 changes: 2 additions & 2 deletions daemon/cmd/daemon_test.go
Expand Up @@ -33,7 +33,7 @@ import (
"github.com/cilium/cilium/pkg/identity/cache"
"github.com/cilium/cilium/pkg/identity/identitymanager"
"github.com/cilium/cilium/pkg/kvstore"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/labelsfilter"
"github.com/cilium/cilium/pkg/lock"
monitorAPI "github.com/cilium/cilium/pkg/monitor/api"
"github.com/cilium/cilium/pkg/option"
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestMain(m *testing.M) {
option.Config.DryMode = true
option.Config.Opts = option.NewIntOptions(&option.DaemonMutableOptionLibrary)
// GetConfig the default labels prefix filter
err := labels.ParseLabelPrefixCfg(nil, "")
err := labelsfilter.ParseLabelPrefixCfg(nil, "")
if err != nil {
panic("ParseLabelPrefixCfg() failed")
}
Expand Down
9 changes: 5 additions & 4 deletions daemon/cmd/endpoint.go
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/cilium/cilium/pkg/k8s"
k8sConst "github.com/cilium/cilium/pkg/k8s/apis/cilium.io"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/labelsfilter"
"github.com/cilium/cilium/pkg/logging/logfields"
monitorAPI "github.com/cilium/cilium/pkg/monitor/api"
"github.com/cilium/cilium/pkg/option"
Expand Down Expand Up @@ -165,7 +166,7 @@ func (d *Daemon) fetchK8sLabelsAndAnnotations(nsName, podName string) (labels.La
}

k8sLbls := labels.Map2Labels(lbls, labels.LabelSourceK8s)
identityLabels, infoLabels := labels.FilterLabels(k8sLbls)
identityLabels, infoLabels := labelsfilter.Filter(k8sLbls)
return identityLabels, infoLabels, annotations, nil
}

Expand Down Expand Up @@ -253,7 +254,7 @@ func (d *Daemon) createEndpoint(ctx context.Context, epTemplate *models.Endpoint
return invalidDataError(ep, fmt.Errorf("not allowed to add reserved labels: %s", lbls))
}

addLabels, _ = labels.FilterLabels(addLabels)
addLabels, _ = labelsfilter.Filter(addLabels)
if len(addLabels) == 0 {
return invalidDataError(ep, fmt.Errorf("no valid labels provided"))
}
Expand Down Expand Up @@ -667,8 +668,8 @@ func (h *getEndpointIDHealthz) Handle(params GetEndpointIDHealthzParams) middlew
// endpoint's labels.
// Returns an HTTP response code and an error msg (or nil on success).
func (d *Daemon) modifyEndpointIdentityLabelsFromAPI(id string, add, del labels.Labels) (int, error) {
addLabels, _ := labels.FilterLabels(add)
delLabels, _ := labels.FilterLabels(del)
addLabels, _ := labelsfilter.Filter(add)
delLabels, _ := labelsfilter.Filter(del)
if lbls := addLabels.FindReserved(); lbls != nil {
return PatchEndpointIDLabelsUpdateFailedCode, fmt.Errorf("Not allowed to add reserved labels: %s", lbls)
} else if lbls := delLabels.FindReserved(); lbls != nil {
Expand Down
5 changes: 3 additions & 2 deletions daemon/cmd/identity.go
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/identity/cache"
"github.com/cilium/cilium/pkg/identity/identitymanager"
identitymodel "github.com/cilium/cilium/pkg/identity/model"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/logging/logfields"

Expand All @@ -46,7 +47,7 @@ func (h *getIdentity) Handle(params GetIdentityParams) middleware.Responder {
return NewGetIdentityIDNotFound()
}

identities = append(identities, identity.GetModel())
identities = append(identities, identitymodel.CreateModel(identity))
}

return NewGetIdentityOK().WithPayload(identities)
Expand All @@ -73,7 +74,7 @@ func (h *getIdentityID) Handle(params GetIdentityIDParams) middleware.Responder
return NewGetIdentityIDNotFound()
}

return NewGetIdentityIDOK().WithPayload(identity.GetModel())
return NewGetIdentityIDOK().WithPayload(identitymodel.CreateModel(identity))
}

type getIdentityEndpoints struct{}
Expand Down
6 changes: 4 additions & 2 deletions pkg/endpoint/api.go
Expand Up @@ -28,8 +28,10 @@ import (
"github.com/cilium/cilium/pkg/endpoint/regeneration"
"github.com/cilium/cilium/pkg/fqdn"
"github.com/cilium/cilium/pkg/identity/cache"
identitymodel "github.com/cilium/cilium/pkg/identity/model"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/labels/model"
"github.com/cilium/cilium/pkg/labelsfilter"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/mac"
"github.com/cilium/cilium/pkg/option"
Expand Down Expand Up @@ -138,7 +140,7 @@ func NewEndpointFromChangeModel(ctx context.Context, owner regeneration.Owner, p

if base.Labels != nil {
lbls := labels.NewLabelsFromModel(base.Labels)
identityLabels, infoLabels := labels.FilterLabels(lbls)
identityLabels, infoLabels := labelsfilter.Filter(lbls)
ep.OpLabels.OrchestrationIdentity = identityLabels
ep.OpLabels.OrchestrationInfo = infoLabels
}
Expand Down Expand Up @@ -197,7 +199,7 @@ func (e *Endpoint) GetModelRLocked() *models.Endpoint {
// FIXME GH-3280 When we begin implementing revision numbers this will
// diverge from models.Endpoint.Spec to reflect the in-datapath config
Realized: spec,
Identity: e.SecurityIdentity.GetModel(),
Identity: identitymodel.CreateModel(e.SecurityIdentity),
Labels: lblMdl,
Networking: &models.EndpointNetworking{
Addressing: []*models.AddressPair{{
Expand Down
4 changes: 2 additions & 2 deletions pkg/endpoint/endpoint_test.go
Expand Up @@ -33,8 +33,8 @@ import (
"github.com/cilium/cilium/pkg/identity/cache"
ciliumio "github.com/cilium/cilium/pkg/k8s/apis/cilium.io"
"github.com/cilium/cilium/pkg/kvstore"
"github.com/cilium/cilium/pkg/labels"
pkgLabels "github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/labelsfilter"
"github.com/cilium/cilium/pkg/lock"
monitorAPI "github.com/cilium/cilium/pkg/monitor/api"
"github.com/cilium/cilium/pkg/option"
Expand Down Expand Up @@ -74,7 +74,7 @@ var _ = Suite(&suite)
func (s *EndpointSuite) SetUpSuite(c *C) {
s.repo = policy.NewPolicyRepository(nil, nil)
// GetConfig the default labels prefix filter
err := labels.ParseLabelPrefixCfg(nil, "")
err := labelsfilter.ParseLabelPrefixCfg(nil, "")
if err != nil {
panic("ParseLabelPrefixCfg() failed")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/endpoint/restore.go
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/ipcache"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/labelsfilter"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/mac"
"github.com/cilium/cilium/pkg/option"
Expand Down Expand Up @@ -186,7 +187,7 @@ func (e *Endpoint) restoreIdentity() error {
}
scopedLog := log.WithField(logfields.EndpointID, e.ID)
// Filter the restored labels with the new daemon's filter
l, _ := labels.FilterLabels(e.OpLabels.AllLabels())
l, _ := labelsfilter.Filter(e.OpLabels.AllLabels())
e.runlock()

allocateCtx, cancel := context.WithTimeout(context.Background(), option.Config.KVstoreConnectivityTimeout)
Expand Down
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package id
package idallocator

import (
"fmt"
Expand Down
Expand Up @@ -14,12 +14,21 @@
//
// +build !privileged_tests

package id
package idallocator

import (
"testing"

. "gopkg.in/check.v1"
)

// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }

type IDSuite struct{}

var _ = Suite(&IDSuite{})

func (s *IDSuite) TestAllocation(c *C) {
ReallocatePool()

Expand Down
9 changes: 5 additions & 4 deletions pkg/endpointmanager/manager.go
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/cilium/cilium/pkg/endpoint"
endpointid "github.com/cilium/cilium/pkg/endpoint/id"
"github.com/cilium/cilium/pkg/endpoint/regeneration"
"github.com/cilium/cilium/pkg/endpointmanager/idallocator"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
Expand Down Expand Up @@ -156,12 +157,12 @@ func (mgr *EndpointManager) InitMetrics() {
func (mgr *EndpointManager) AllocateID(currID uint16) (uint16, error) {
var newID uint16
if currID != 0 {
if err := endpointid.Reuse(currID); err != nil {
if err := idallocator.Reuse(currID); err != nil {
return 0, fmt.Errorf("unable to reuse endpoint ID: %s", err)
}
newID = currID
} else {
id := endpointid.Allocate()
id := idallocator.Allocate()
if id == uint16(0) {
return 0, fmt.Errorf("no more endpoint IDs available")
}
Expand Down Expand Up @@ -278,7 +279,7 @@ func (mgr *EndpointManager) LookupPodName(name string) *endpoint.Endpoint {
// ReleaseID releases the ID of the specified endpoint from the EndpointManager.
// Returns an error if the ID cannot be released.
func (mgr *EndpointManager) ReleaseID(ep *endpoint.Endpoint) error {
return endpointid.Release(ep.ID)
return idallocator.Release(ep.ID)
}

// WaitEndpointRemoved waits until all operations associated with Remove of
Expand All @@ -291,7 +292,7 @@ func (mgr *EndpointManager) WaitEndpointRemoved(ep *endpoint.Endpoint) {
func (mgr *EndpointManager) RemoveAll() {
mgr.mutex.Lock()
defer mgr.mutex.Unlock()
endpointid.ReallocatePool()
idallocator.ReallocatePool()
mgr.endpoints = map[uint16]*endpoint.Endpoint{}
mgr.endpointsAux = map[string]*endpoint.Endpoint{}
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/identity/cache/cache.go
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/allocator"
"github.com/cilium/cilium/pkg/identity"
identitymodel "github.com/cilium/cilium/pkg/identity/model"
"github.com/cilium/cilium/pkg/idpool"
"github.com/cilium/cilium/pkg/kvstore"
"github.com/cilium/cilium/pkg/labels"
Expand Down Expand Up @@ -84,17 +85,17 @@ func (m *CachingIdentityAllocator) GetIdentities() IdentitiesModel {
m.IdentityAllocator.ForeachCache(func(id idpool.ID, val allocator.AllocatorKey) {
if gi, ok := val.(GlobalIdentity); ok {
identity := identity.NewIdentityFromLabelArray(identity.NumericIdentity(id), gi.LabelArray)
identities = append(identities, identity.GetModel())
identities = append(identities, identitymodel.CreateModel(identity))
}

})
// append user reserved identities
for _, v := range identity.ReservedIdentityCache {
identities = append(identities, v.GetModel())
identities = append(identities, identitymodel.CreateModel(v))
}

for _, v := range m.localIdentities.GetIdentities() {
identities = append(identities, v.GetModel())
identities = append(identities, identitymodel.CreateModel(v))
}

return identities
Expand Down
37 changes: 0 additions & 37 deletions pkg/identity/identity.go
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"
"net"

"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/labels"
)

Expand Down Expand Up @@ -70,24 +69,6 @@ type IPIdentityPair struct {
K8sPodName string `json:"K8sPodName,omitempty"`
}

func NewIdentityFromModel(base *models.Identity) *Identity {
if base == nil {
return nil
}

id := &Identity{
ID: NumericIdentity(base.ID),
Labels: make(labels.Labels),
}
for _, v := range base.Labels {
lbl := labels.ParseLabel(v)
id.Labels[lbl.Key] = lbl
}
id.Sanitize()

return id
}

// Sanitize takes a partially initialized Identity (for example, deserialized
// from json) and reconstitutes the full object from what has been restored.
func (id *Identity) Sanitize() {
Expand Down Expand Up @@ -116,24 +97,6 @@ func (id *Identity) String() string {
return id.ID.StringID()
}

func (id *Identity) GetModel() *models.Identity {
if id == nil {
return nil
}

ret := &models.Identity{
ID: int64(id.ID),
Labels: []string{},
LabelsSHA256: "",
}

for _, v := range id.Labels {
ret.Labels = append(ret.Labels, v.String())
}
ret.LabelsSHA256 = id.GetLabelsSHA256()
return ret
}

// IsReserved returns whether the identity represents a reserved identity
// (true), or not (false).
func (id *Identity) IsReserved() bool {
Expand Down
3 changes: 2 additions & 1 deletion pkg/identity/identitymanager/manager.go
Expand Up @@ -17,6 +17,7 @@ package identitymanager
import (
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/identity/model"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging/logfields"

Expand Down Expand Up @@ -187,7 +188,7 @@ func (idm *IdentityManager) GetIdentityModels() []*models.IdentityEndpoints {

for _, v := range idm.identities {
identities = append(identities, &models.IdentityEndpoints{
Identity: v.identity.GetModel(),
Identity: model.CreateModel(v.identity),
RefCount: int64(v.refCount),
})
}
Expand Down