diff --git a/agent/consul/state/peering.go b/agent/consul/state/peering.go index 34f99d06bd..af5098987c 100644 --- a/agent/consul/state/peering.go +++ b/agent/consul/state/peering.go @@ -226,9 +226,9 @@ func (s *Store) PeeringWrite(idx uint64, p *pbpeering.Peering) error { if !p.IsActive() { return fmt.Errorf("cannot create a new peering marked for deletion") } - - // TODO(peering): consider keeping PeeringState enum elsewhere? - p.State = pbpeering.PeeringState_INITIAL + if p.State == 0 { + p.State = pbpeering.PeeringState_PENDING + } p.CreateIndex = idx p.ModifyIndex = idx } diff --git a/agent/consul/state/peering_test.go b/agent/consul/state/peering_test.go index 0f7e6dc9d0..7ef206cf1c 100644 --- a/agent/consul/state/peering_test.go +++ b/agent/consul/state/peering_test.go @@ -31,7 +31,7 @@ func insertTestPeerings(t *testing.T, s *Store) { Name: "foo", Partition: structs.NodeEnterpriseMetaInDefaultPartition().PartitionOrEmpty(), ID: testFooPeerID, - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_PENDING, CreateIndex: 1, ModifyIndex: 1, }) @@ -111,7 +111,7 @@ func TestStateStore_PeeringReadByID(t *testing.T) { Name: "foo", Partition: structs.NodeEnterpriseMetaInDefaultPartition().PartitionOrEmpty(), ID: testFooPeerID, - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_PENDING, CreateIndex: 1, ModifyIndex: 1, }, @@ -165,7 +165,7 @@ func TestStateStore_PeeringRead(t *testing.T) { Name: "foo", Partition: structs.NodeEnterpriseMetaInDefaultPartition().PartitionOrEmpty(), ID: testFooPeerID, - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_PENDING, CreateIndex: 1, ModifyIndex: 1, }, @@ -303,7 +303,7 @@ func TestStore_PeeringList(t *testing.T) { Name: "foo", Partition: structs.NodeEnterpriseMetaInDefaultPartition().PartitionOrEmpty(), ID: testFooPeerID, - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_PENDING, CreateIndex: 1, ModifyIndex: 1, }, @@ -423,10 +423,7 @@ func TestStore_PeeringWrite(t *testing.T) { _, p, err := s.PeeringRead(nil, q) require.NoError(t, err) require.NotNil(t, p) - - if tc.input.State == 0 { - require.Equal(t, pbpeering.PeeringState_INITIAL, p.State) - } + require.Equal(t, tc.input.State, p.State) require.Equal(t, tc.input.Name, p.Name) } tcs := []testcase{ @@ -1079,7 +1076,7 @@ func TestStateStore_PeeringsForService(t *testing.T) { { peering: &pbpeering.Peering{ Name: "peer1", - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_PENDING, }, }, { @@ -1108,7 +1105,7 @@ func TestStateStore_PeeringsForService(t *testing.T) { query: []string{"foo"}, expect: [][]*pbpeering.Peering{ { - {Name: "peer1", State: pbpeering.PeeringState_INITIAL}, + {Name: "peer1", State: pbpeering.PeeringState_PENDING}, }, }, expectIdx: uint64(6), // config entries max index @@ -1123,13 +1120,13 @@ func TestStateStore_PeeringsForService(t *testing.T) { { peering: &pbpeering.Peering{ Name: "peer1", - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_PENDING, }, }, { peering: &pbpeering.Peering{ Name: "peer2", - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_PENDING, }, }, }, @@ -1157,10 +1154,10 @@ func TestStateStore_PeeringsForService(t *testing.T) { query: []string{"foo", "bar"}, expect: [][]*pbpeering.Peering{ { - {Name: "peer1", State: pbpeering.PeeringState_INITIAL}, + {Name: "peer1", State: pbpeering.PeeringState_PENDING}, }, { - {Name: "peer2", State: pbpeering.PeeringState_INITIAL}, + {Name: "peer2", State: pbpeering.PeeringState_PENDING}, }, }, expectIdx: uint64(6), // config entries max index @@ -1175,19 +1172,19 @@ func TestStateStore_PeeringsForService(t *testing.T) { { peering: &pbpeering.Peering{ Name: "peer1", - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_PENDING, }, }, { peering: &pbpeering.Peering{ Name: "peer2", - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_PENDING, }, }, { peering: &pbpeering.Peering{ Name: "peer3", - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_PENDING, }, }, }, @@ -1218,11 +1215,11 @@ func TestStateStore_PeeringsForService(t *testing.T) { query: []string{"foo", "bar"}, expect: [][]*pbpeering.Peering{ { - {Name: "peer1", State: pbpeering.PeeringState_INITIAL}, - {Name: "peer2", State: pbpeering.PeeringState_INITIAL}, + {Name: "peer1", State: pbpeering.PeeringState_PENDING}, + {Name: "peer2", State: pbpeering.PeeringState_PENDING}, }, { - {Name: "peer3", State: pbpeering.PeeringState_INITIAL}, + {Name: "peer3", State: pbpeering.PeeringState_PENDING}, }, }, expectIdx: uint64(7), diff --git a/agent/peering_endpoint_test.go b/agent/peering_endpoint_test.go index 88f05172a0..b9aee1ab45 100644 --- a/agent/peering_endpoint_test.go +++ b/agent/peering_endpoint_test.go @@ -217,7 +217,7 @@ func TestHTTP_Peering_MethodNotAllowed(t *testing.T) { foo := &pbpeering.PeeringWriteRequest{ Peering: &pbpeering.Peering{ Name: "foo", - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_ESTABLISHING, PeerCAPems: nil, PeerServerName: "fooservername", PeerServerAddresses: []string{"addr1"}, @@ -252,7 +252,7 @@ func TestHTTP_Peering_Read(t *testing.T) { foo := &pbpeering.PeeringWriteRequest{ Peering: &pbpeering.Peering{ Name: "foo", - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_ESTABLISHING, PeerCAPems: nil, PeerServerName: "fooservername", PeerServerAddresses: []string{"addr1"}, @@ -317,7 +317,7 @@ func TestHTTP_Peering_Delete(t *testing.T) { foo := &pbpeering.PeeringWriteRequest{ Peering: &pbpeering.Peering{ Name: "foo", - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_ESTABLISHING, PeerCAPems: nil, PeerServerName: "fooservername", PeerServerAddresses: []string{"addr1"}, @@ -387,7 +387,7 @@ func TestHTTP_Peering_List(t *testing.T) { foo := &pbpeering.PeeringWriteRequest{ Peering: &pbpeering.Peering{ Name: "foo", - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_ESTABLISHING, PeerCAPems: nil, PeerServerName: "fooservername", PeerServerAddresses: []string{"addr1"}, diff --git a/agent/rpc/peering/service.go b/agent/rpc/peering/service.go index 3bcafa986e..3720ec135d 100644 --- a/agent/rpc/peering/service.go +++ b/agent/rpc/peering/service.go @@ -309,6 +309,7 @@ func (s *Service) Establish( PeerServerName: tok.ServerName, PeerID: tok.PeerID, Meta: req.Meta, + State: pbpeering.PeeringState_ESTABLISHING, }, } if err = s.Backend.Apply().PeeringWrite(writeReq); err != nil { @@ -481,6 +482,7 @@ func (s *Service) PeeringDelete(ctx context.Context, req *pbpeering.PeeringDelet ID: existing.ID, Name: req.Name, Partition: req.Partition, + State: pbpeering.PeeringState_DELETING, DeletedAt: structs.TimeToProto(time.Now().UTC()), }, } diff --git a/agent/rpc/peering/service_test.go b/agent/rpc/peering/service_test.go index cd6d843325..ca9737f656 100644 --- a/agent/rpc/peering/service_test.go +++ b/agent/rpc/peering/service_test.go @@ -108,7 +108,7 @@ func TestPeeringService_GenerateToken(t *testing.T) { Name: "peerB", Partition: acl.DefaultPartitionName, ID: token.PeerID, - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_PENDING, Meta: map[string]string{"foo": "bar"}, } require.Equal(t, expect, peers[0]) @@ -208,7 +208,7 @@ func TestPeeringService_Establish(t *testing.T) { expectResp: &pbpeering.EstablishResponse{}, expectPeering: peering.TestPeering( "peer1-usw1", - pbpeering.PeeringState_INITIAL, + pbpeering.PeeringState_ESTABLISHING, map[string]string{"foo": "bar"}, ), }, @@ -228,7 +228,7 @@ func TestPeeringService_Read(t *testing.T) { p := &pbpeering.Peering{ ID: testUUID(t), Name: "foo", - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_ESTABLISHING, PeerCAPems: nil, PeerServerName: "test", PeerServerAddresses: []string{"addr1"}, @@ -286,7 +286,7 @@ func TestPeeringService_Delete(t *testing.T) { p := &pbpeering.Peering{ ID: testUUID(t), Name: "foo", - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_ESTABLISHING, PeerCAPems: nil, PeerServerName: "test", PeerServerAddresses: []string{"addr1"}, @@ -324,7 +324,7 @@ func TestPeeringService_List(t *testing.T) { foo := &pbpeering.Peering{ ID: testUUID(t), Name: "foo", - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_ESTABLISHING, PeerCAPems: nil, PeerServerName: "fooservername", PeerServerAddresses: []string{"addr1"}, @@ -419,7 +419,7 @@ func TestPeeringService_TrustBundleListByService(t *testing.T) { require.NoError(t, s.Server.FSM().State().PeeringWrite(lastIdx, &pbpeering.Peering{ ID: testUUID(t), Name: "foo", - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_ESTABLISHING, PeerServerName: "test", PeerServerAddresses: []string{"addr1"}, })) @@ -428,7 +428,7 @@ func TestPeeringService_TrustBundleListByService(t *testing.T) { require.NoError(t, s.Server.FSM().State().PeeringWrite(lastIdx, &pbpeering.Peering{ ID: testUUID(t), Name: "bar", - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_ESTABLISHING, PeerServerName: "test-bar", PeerServerAddresses: []string{"addr2"}, })) diff --git a/agent/ui_endpoint_test.go b/agent/ui_endpoint_test.go index 0ddb180ac8..7d96430388 100644 --- a/agent/ui_endpoint_test.go +++ b/agent/ui_endpoint_test.go @@ -116,7 +116,7 @@ func TestUINodes(t *testing.T) { peerOne := &pbpeering.PeeringWriteRequest{ Peering: &pbpeering.Peering{ Name: "peer1", - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_ESTABLISHING, PeerCAPems: nil, PeerServerName: "fooservername", PeerServerAddresses: []string{"addr1"}, @@ -281,7 +281,7 @@ func TestUIServices(t *testing.T) { }, }, }, - //register api service on node foo + // register api service on node foo { Datacenter: "dc1", Node: "foo", @@ -400,7 +400,7 @@ func TestUIServices(t *testing.T) { peerOne := &pbpeering.PeeringWriteRequest{ Peering: &pbpeering.Peering{ Name: "peer1", - State: pbpeering.PeeringState_INITIAL, + State: pbpeering.PeeringState_ESTABLISHING, PeerCAPems: nil, PeerServerName: "fooservername", PeerServerAddresses: []string{"addr1"}, diff --git a/api/peering.go b/api/peering.go index d4b2bc717f..0cfbae9fd1 100644 --- a/api/peering.go +++ b/api/peering.go @@ -14,9 +14,14 @@ const ( // writes. PeeringStateUndefined PeeringState = "UNDEFINED" - // PeeringStateInitial means a Peering has been initialized and is awaiting - // acknowledgement from a remote peer. - PeeringStateInitial PeeringState = "INITIAL" + // PeeringStatePending means the peering was created by generating a peering token. + // Peerings stay in a pending state until the peer uses the token to dial + // the local cluster. + PeeringStatePending PeeringState = "PENDING" + + // PeeringStateEstablishing means the peering is being established from a peering token. + // This is the initial state for dialing peers. + PeeringStateEstablishing PeeringState = "ESTABLISHING" // PeeringStateActive means that the peering connection is active and // healthy. @@ -26,6 +31,10 @@ const ( // but has not yet been terminated. PeeringStateFailing PeeringState = "FAILING" + // PeeringStateDeleting means a peering was marked for deletion and is in the process + // of being deleted. + PeeringStateDeleting PeeringState = "DELETING" + // PeeringStateTerminated means the peering relationship has been removed. PeeringStateTerminated PeeringState = "TERMINATED" ) diff --git a/api/peering_test.go b/api/peering_test.go index 963f05f06c..1c022a9cf2 100644 --- a/api/peering_test.go +++ b/api/peering_test.go @@ -183,7 +183,7 @@ func TestAPI_Peering_GenerateToken_Read_Establish_Delete(t *testing.T) { // token specific assertions on the "server" require.Equal(t, "peer1", resp.Name) - require.Equal(t, PeeringStateInitial, resp.State) + require.Equal(t, PeeringStatePending, resp.State) require.Equal(t, map[string]string{"foo": "bar"}, resp.Meta) }) @@ -205,7 +205,7 @@ func TestAPI_Peering_GenerateToken_Read_Establish_Delete(t *testing.T) { require.NotNil(r, qm) // require that the peering state is not undefined - require.Equal(r, PeeringStateInitial, resp.State) + require.Equal(r, PeeringStateEstablishing, resp.State) require.Equal(r, map[string]string{"foo": "bar"}, resp.Meta) }) }) diff --git a/proto/pbpeering/peering.go b/proto/pbpeering/peering.go index f090ebef74..cb26b2a56b 100644 --- a/proto/pbpeering/peering.go +++ b/proto/pbpeering/peering.go @@ -144,12 +144,16 @@ func (b *PeeringTrustBundle) ConcatenatedRootPEMs() string { // enumcover:PeeringState func PeeringStateToAPI(s PeeringState) api.PeeringState { switch s { - case PeeringState_INITIAL: - return api.PeeringStateInitial + case PeeringState_PENDING: + return api.PeeringStatePending + case PeeringState_ESTABLISHING: + return api.PeeringStateEstablishing case PeeringState_ACTIVE: return api.PeeringStateActive case PeeringState_FAILING: return api.PeeringStateFailing + case PeeringState_DELETING: + return api.PeeringStateDeleting case PeeringState_TERMINATED: return api.PeeringStateTerminated case PeeringState_UNDEFINED: @@ -162,12 +166,16 @@ func PeeringStateToAPI(s PeeringState) api.PeeringState { // enumcover:api.PeeringState func PeeringStateFromAPI(t api.PeeringState) PeeringState { switch t { - case api.PeeringStateInitial: - return PeeringState_INITIAL + case api.PeeringStatePending: + return PeeringState_PENDING + case api.PeeringStateEstablishing: + return PeeringState_ESTABLISHING case api.PeeringStateActive: return PeeringState_ACTIVE case api.PeeringStateFailing: return PeeringState_FAILING + case api.PeeringStateDeleting: + return PeeringState_DELETING case api.PeeringStateTerminated: return PeeringState_TERMINATED case api.PeeringStateUndefined: diff --git a/proto/pbpeering/peering.pb.go b/proto/pbpeering/peering.pb.go index 86d935b4cc..98d1c4382c 100644 --- a/proto/pbpeering/peering.pb.go +++ b/proto/pbpeering/peering.pb.go @@ -30,33 +30,44 @@ const ( // Undefined represents an unset value for PeeringState during // writes. PeeringState_UNDEFINED PeeringState = 0 - // Initial means a Peering has been initialized and is awaiting - // acknowledgement from a remote peer. - PeeringState_INITIAL PeeringState = 1 + // Pending means the peering was created by generating a peering token. + // Peerings stay in a pending state until the peer uses the token to dial + // the local cluster. + PeeringState_PENDING PeeringState = 1 + // Establishing means the peering is being established from a peering token. + // This is the initial state for dialing peers. + PeeringState_ESTABLISHING PeeringState = 2 // Active means that the peering connection is active and healthy. - PeeringState_ACTIVE PeeringState = 2 + PeeringState_ACTIVE PeeringState = 3 // Failing means the peering connection has been interrupted but has not yet // been terminated. - PeeringState_FAILING PeeringState = 3 + PeeringState_FAILING PeeringState = 4 + // Deleting means a peering was marked for deletion and is in the process + // of being deleted. + PeeringState_DELETING PeeringState = 5 // Terminated means the peering relationship has been removed. - PeeringState_TERMINATED PeeringState = 4 + PeeringState_TERMINATED PeeringState = 6 ) // Enum value maps for PeeringState. var ( PeeringState_name = map[int32]string{ 0: "UNDEFINED", - 1: "INITIAL", - 2: "ACTIVE", - 3: "FAILING", - 4: "TERMINATED", + 1: "PENDING", + 2: "ESTABLISHING", + 3: "ACTIVE", + 4: "FAILING", + 5: "DELETING", + 6: "TERMINATED", } PeeringState_value = map[string]int32{ - "UNDEFINED": 0, - "INITIAL": 1, - "ACTIVE": 2, - "FAILING": 3, - "TERMINATED": 4, + "UNDEFINED": 0, + "PENDING": 1, + "ESTABLISHING": 2, + "ACTIVE": 3, + "FAILING": 4, + "DELETING": 5, + "TERMINATED": 6, } ) @@ -2272,12 +2283,14 @@ var file_proto_pbpeering_peering_proto_rawDesc = []byte{ 0x65, 0x64, 0x42, 0x09, 0x0a, 0x07, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x22, 0x29, 0x0a, 0x0d, 0x4c, 0x65, 0x61, 0x64, 0x65, 0x72, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x12, 0x18, 0x0a, 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x2a, 0x53, 0x0a, 0x0c, 0x50, 0x65, 0x65, 0x72, + 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x2a, 0x73, 0x0a, 0x0c, 0x50, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x0d, 0x0a, 0x09, 0x55, 0x4e, 0x44, 0x45, - 0x46, 0x49, 0x4e, 0x45, 0x44, 0x10, 0x00, 0x12, 0x0b, 0x0a, 0x07, 0x49, 0x4e, 0x49, 0x54, 0x49, - 0x41, 0x4c, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x41, 0x43, 0x54, 0x49, 0x56, 0x45, 0x10, 0x02, - 0x12, 0x0b, 0x0a, 0x07, 0x46, 0x41, 0x49, 0x4c, 0x49, 0x4e, 0x47, 0x10, 0x03, 0x12, 0x0e, 0x0a, - 0x0a, 0x54, 0x45, 0x52, 0x4d, 0x49, 0x4e, 0x41, 0x54, 0x45, 0x44, 0x10, 0x04, 0x32, 0xed, 0x05, + 0x46, 0x49, 0x4e, 0x45, 0x44, 0x10, 0x00, 0x12, 0x0b, 0x0a, 0x07, 0x50, 0x45, 0x4e, 0x44, 0x49, + 0x4e, 0x47, 0x10, 0x01, 0x12, 0x10, 0x0a, 0x0c, 0x45, 0x53, 0x54, 0x41, 0x42, 0x4c, 0x49, 0x53, + 0x48, 0x49, 0x4e, 0x47, 0x10, 0x02, 0x12, 0x0a, 0x0a, 0x06, 0x41, 0x43, 0x54, 0x49, 0x56, 0x45, + 0x10, 0x03, 0x12, 0x0b, 0x0a, 0x07, 0x46, 0x41, 0x49, 0x4c, 0x49, 0x4e, 0x47, 0x10, 0x04, 0x12, + 0x0c, 0x0a, 0x08, 0x44, 0x45, 0x4c, 0x45, 0x54, 0x49, 0x4e, 0x47, 0x10, 0x05, 0x12, 0x0e, 0x0a, + 0x0a, 0x54, 0x45, 0x52, 0x4d, 0x49, 0x4e, 0x41, 0x54, 0x45, 0x44, 0x10, 0x06, 0x32, 0xed, 0x05, 0x0a, 0x0e, 0x50, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x4e, 0x0a, 0x0d, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x1d, 0x2e, 0x70, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x2e, 0x47, 0x65, 0x6e, 0x65, diff --git a/proto/pbpeering/peering.proto b/proto/pbpeering/peering.proto index ea7b1c58c1..3e5d2397fc 100644 --- a/proto/pbpeering/peering.proto +++ b/proto/pbpeering/peering.proto @@ -39,19 +39,28 @@ enum PeeringState { // writes. UNDEFINED = 0; - // Initial means a Peering has been initialized and is awaiting - // acknowledgement from a remote peer. - INITIAL = 1; + // Pending means the peering was created by generating a peering token. + // Peerings stay in a pending state until the peer uses the token to dial + // the local cluster. + PENDING = 1; + + // Establishing means the peering is being established from a peering token. + // This is the initial state for dialing peers. + ESTABLISHING = 2; // Active means that the peering connection is active and healthy. - ACTIVE = 2; + ACTIVE = 3; // Failing means the peering connection has been interrupted but has not yet // been terminated. - FAILING = 3; + FAILING = 4; + + // Deleting means a peering was marked for deletion and is in the process + // of being deleted. + DELETING = 5; // Terminated means the peering relationship has been removed. - TERMINATED = 4; + TERMINATED = 6; } // Peering defines a peering relationship between two disparate Consul clusters