This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
PMM-5194 Tunnels #643
Draft
AlekSi
wants to merge
18
commits into
main
Choose a base branch
from
PMM-5194-tunnels
base: main
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.
Draft
PMM-5194 Tunnels #643
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
846ce69
PMM-5194 Tweak comments
AlekSi 84339b3
WIP
AlekSi d8f4768
WIP
AlekSi 342a9ac
PMM-5194 Add metric
AlekSi 6975404
PMM-5194 Do not keep disconnected agents in memory
AlekSi 67ed674
Merge branch 'PMM-5194-channel-metrics' into PMM-5194-tunnels
AlekSi f3dca60
Merge branch 'PMM-2.0' into PMM-5194-tunnels
AlekSi b485550
WIP
AlekSi 070eb51
Merge branch 'PMM-2.0' into PMM-5194-tunnels
AlekSi 1bb12ec
WIP
AlekSi c258bac
WIP
AlekSi 0657a77
WIP
AlekSi ed86ae0
WIP
AlekSi 7f5a18e
WIP
AlekSi 18929c5
WIP
AlekSi b8cbd17
WIP
AlekSi ea2a2fb
WIP
AlekSi a98489a
WIP
AlekSi 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,127 @@ | ||
// pmm-managed | ||
// Copyright (C) 2017 Percona LLC | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package models | ||
|
||
import ( | ||
"github.com/google/uuid" | ||
"github.com/pkg/errors" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"gopkg.in/reform.v1" | ||
) | ||
|
||
func checkUniqueTunnelID(q *reform.Querier, id string) error { | ||
if id == "" { | ||
panic("empty Tunnel ID") | ||
} | ||
|
||
tunnel := &Tunnel{TunnelID: id} | ||
switch err := q.Reload(tunnel); err { | ||
case nil: | ||
return status.Errorf(codes.AlreadyExists, "Tunnel with ID %q already exists.", id) | ||
case reform.ErrNoRows: | ||
return nil | ||
default: | ||
return errors.WithStack(err) | ||
} | ||
} | ||
|
||
// FindTunnels returns Tunnels for given pmm-agent, or all, if pmmAgentID is empty. | ||
func FindTunnels(q *reform.Querier, pmmAgentID string) ([]*Tunnel, error) { | ||
var args []interface{} | ||
tail := "ORDER BY tunnel_id" | ||
if pmmAgentID != "" { | ||
// TODO check that agent exist | ||
args = []interface{}{pmmAgentID, pmmAgentID} | ||
tail = "WHERE listen_agent_id = $1 OR connect_agent_id = $2 " + tail | ||
} | ||
|
||
structs, err := q.SelectAllFrom(TunnelTable, tail, args...) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
tunnels := make([]*Tunnel, len(structs)) | ||
for i, s := range structs { | ||
tunnels[i] = s.(*Tunnel) | ||
} | ||
|
||
return tunnels, nil | ||
} | ||
|
||
// FindTunnelByID finds Tunnel by ID. | ||
func FindTunnelByID(q *reform.Querier, id string) (*Tunnel, error) { | ||
if id == "" { | ||
return nil, status.Error(codes.InvalidArgument, "Empty Tunnel ID.") | ||
} | ||
|
||
tunnel := &Tunnel{TunnelID: id} | ||
switch err := q.Reload(tunnel); err { | ||
case nil: | ||
return tunnel, nil | ||
case reform.ErrNoRows: | ||
return nil, status.Errorf(codes.NotFound, "Tunnel with ID %q not found.", id) | ||
default: | ||
return nil, errors.WithStack(err) | ||
} | ||
} | ||
|
||
// CreateTunnelParams TODO. | ||
type CreateTunnelParams struct { | ||
ListenAgentID string | ||
ListenPort uint16 | ||
ConnectAgentID string | ||
ConnectPort uint16 | ||
} | ||
|
||
// CreateTunnel creates Tunnel. | ||
func CreateTunnel(q *reform.Querier, params *CreateTunnelParams) (*Tunnel, error) { | ||
id := "/tunnel_id/" + uuid.New().String() | ||
if err := checkUniqueTunnelID(q, id); err != nil { | ||
return nil, err | ||
} | ||
|
||
// TODO check that agents exist | ||
// TODO check that ports > 0 | ||
|
||
row := &Tunnel{ | ||
TunnelID: id, | ||
ListenAgentID: params.ListenAgentID, | ||
ListenPort: params.ListenPort, | ||
ConnectAgentID: params.ConnectAgentID, | ||
ConnectPort: params.ConnectPort, | ||
} | ||
|
||
if err := q.Insert(row); err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
return row, nil | ||
} | ||
|
||
// RemoveTunnel removes Tunnel by ID. | ||
func RemoveTunnel(q *reform.Querier, id string, mode RemoveMode) (*Tunnel, error) { | ||
// TODO find agents | ||
// TODO cascade delete | ||
|
||
t := &Tunnel{TunnelID: id} | ||
if err := q.Delete(t); err != nil { | ||
return nil, errors.Wrap(err, "failed to delete Tunnel") | ||
} | ||
|
||
return t, 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,64 @@ | ||
// pmm-managed | ||
// Copyright (C) 2017 Percona LLC | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package models | ||
|
||
import ( | ||
"time" | ||
|
||
"gopkg.in/reform.v1" | ||
) | ||
|
||
//go:generate reform | ||
|
||
//reform:tunnels | ||
type Tunnel struct { | ||
TunnelID string `reform:"tunnel_id,pk"` | ||
ListenAgentID string `reform:"listen_agent_id"` | ||
ListenPort uint16 `reform:"listen_port"` | ||
ConnectAgentID string `reform:"connect_agent_id"` | ||
ConnectPort uint16 `reform:"connect_port"` | ||
CreatedAt time.Time `reform:"created_at"` | ||
UpdatedAt time.Time `reform:"updated_at"` | ||
} | ||
|
||
// BeforeInsert implements reform.BeforeInserter interface. | ||
func (s *Tunnel) BeforeInsert() error { | ||
now := Now() | ||
s.CreatedAt = now | ||
s.UpdatedAt = now | ||
return nil | ||
} | ||
|
||
// BeforeUpdate implements reform.BeforeUpdater interface. | ||
func (s *Tunnel) BeforeUpdate() error { | ||
s.UpdatedAt = Now() | ||
return nil | ||
} | ||
|
||
// AfterFind implements reform.AfterFinder interface. | ||
func (s *Tunnel) AfterFind() error { | ||
s.CreatedAt = s.CreatedAt.UTC() | ||
s.UpdatedAt = s.UpdatedAt.UTC() | ||
return nil | ||
} | ||
|
||
// check interfaces | ||
var ( | ||
_ reform.BeforeInserter = (*Tunnel)(nil) | ||
_ reform.BeforeUpdater = (*Tunnel)(nil) | ||
_ reform.AfterFinder = (*Tunnel)(nil) | ||
) |
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.
🚫 [golangci-lint] reported by reviewdog 🐶
comment on exported type Tunnel should be of the form "Tunnel ..." (with optional leading article) (golint)