Class: EksCli::VPC::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/eks_cli/vpc/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(cluster_name) ⇒ Client

Returns a new instance of Client.



8
9
10
# File 'lib/eks_cli/vpc/client.rb', line 8

def initialize(cluster_name)
  @cluster_name = cluster_name
end

Instance Method Details

#allow_networking(old_vpc_sg_id, peering_connection_id) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/eks_cli/vpc/client.rb', line 55

def allow_networking(old_vpc_sg_id, peering_connection_id)
  Log.info "allowing incoming traffic to sg #{old_vpc_sg_id} from #{config["nodes_sg_id"]} on vpc #{new_vpc.id}"
  old_sg  = Aws::EC2::SecurityGroup.new(old_vpc_sg_id, client: client)
  res = old_sg.authorize_ingress(
    ip_permissions: [
      {
        from_port: "-1",
        ip_protocol: "-1",
        to_port: "-1",
        user_id_group_pairs: [
          {
            description: "Accept all traffic from nodes on EKS cluster #{@cluster_name}",
            group_id: config["nodes_sg_id"],
            vpc_id: new_vpc.id,
            vpc_peering_connection_id: peering_connection_id,
          },
        ],
      },
    ]
  )
  Log.info "done setting networking (#{res})"
end

#clientObject



107
108
109
# File 'lib/eks_cli/vpc/client.rb', line 107

def client
  @client ||= Aws::EC2::Client.new(region: config["region"])
end

#configObject



103
104
105
# File 'lib/eks_cli/vpc/client.rb', line 103

def config
  @config ||= Config[@cluster_name]
end

#create_vpc_peering_connectionObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/eks_cli/vpc/client.rb', line 21

def create_vpc_peering_connection
  Log.info "creating VPC peering request between #{new_vpc.id} and #{old_vpc.id}"
  pcr = client.create_vpc_peering_connection({
    dry_run: false,
    peer_vpc_id: old_vpc.id,
    vpc_id: new_vpc.id,
  })
  Log.info "created peering request #{pcr}"
  peering_connection_id = pcr.vpc_peering_connection.vpc_peering_connection_id
  Log.info "accepting peering request"
  res = client.accept_vpc_peering_connection({
    dry_run: false,
    vpc_peering_connection_id: peering_connection_id,
  })
  Log.info "request accepted: #{res}"
  return peering_connection_id
end

#delete_vpc_peering_connectionObject



39
40
41
42
43
44
45
46
# File 'lib/eks_cli/vpc/client.rb', line 39

def delete_vpc_peering_connection
  if id = config["vpc_peering_connection_id"]
    Log.info "deleting vpc peering connection #{id}"
    Log.info client.delete_vpc_peering_connection(vpc_peering_connection_id: id)
  else
    Log.info "no vpc peering connection found"
  end
end

#new_vpcObject



91
92
93
# File 'lib/eks_cli/vpc/client.rb', line 91

def new_vpc
  @new_vpc ||= vpc_by_id(new_vpc_id)
end

#new_vpc_idObject



111
112
113
# File 'lib/eks_cli/vpc/client.rb', line 111

def new_vpc_id
  @new_vpc_id ||= config["vpc_id"]
end

#old_vpcObject



95
96
97
# File 'lib/eks_cli/vpc/client.rb', line 95

def old_vpc
  @old_vpc
end

#old_vpc_idObject



115
116
117
# File 'lib/eks_cli/vpc/client.rb', line 115

def old_vpc_id
  @old_vpc_id
end

#point_from(from_vpc, to_vpc, peering_connection_id) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/eks_cli/vpc/client.rb', line 78

def point_from(from_vpc, to_vpc, peering_connection_id)
  Log.info "pointing from #{from_vpc.id} to #{to_vpc.id} via #{peering_connection_id}"
  from_vpc.route_tables.each do |rt|
    res = client.create_route({
      destination_cidr_block: to_vpc.cidr_block, 
      gateway_id: peering_connection_id, 
      route_table_id: rt.id, 
    })
    Log.info "set route #{res}"
  end

end

#set_inter_vpc_networking(old_vpc_id, old_vpc_sg_id) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/eks_cli/vpc/client.rb', line 12

def set_inter_vpc_networking(old_vpc_id, old_vpc_sg_id)
  @old_vpc = vpc_by_id(old_vpc_id)
  Log.info "setting vpc networking between #{new_vpc.id} and #{old_vpc.id}"
  peering_connection_id = create_vpc_peering_connection
  config.write(vpc_peering_connection_id: peering_connection_id)
  update_route_tables(peering_connection_id)
  allow_networking(old_vpc_sg_id, peering_connection_id)
end

#update_route_tables(peering_connection_id) ⇒ Object



48
49
50
51
52
53
# File 'lib/eks_cli/vpc/client.rb', line 48

def update_route_tables(peering_connection_id)
  Log.info "updating route tables"
  point_from(old_vpc, new_vpc, peering_connection_id)
  point_from(new_vpc, old_vpc, peering_connection_id)
  Log.info "done updating route tables"
end

#vpc_by_id(id) ⇒ Object



99
100
101
# File 'lib/eks_cli/vpc/client.rb', line 99

def vpc_by_id(id)
  Aws::EC2::Vpc.new(id, client: client)
end