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

refactor(transparent-proxy): allow config to be initialized #10207

Merged

Conversation

bartsmykla
Copy link
Contributor

Summary

Introduced InitializedConfig structure

InitializedConfig extends the Config struct by adding fields that require additional logic to retrieve their values. These values typically involve interacting with the system or external resources.

An InitializedConfig can contain nested configuration structs. If these nested structs share field names with the Config struct, they should embed the corresponding field from Config during initialization. I.e.

type Redirect struct {
	// NamePrefix is a prefix which will be used go generate chains name
	NamePrefix string
	Inbound    TrafficFlow
	Outbound   TrafficFlow
	DNS        DNS
	VNet       VNet
}

type InitializedRedirect struct {
	Redirect
	DNS InitializedDNS
}

func (c Redirect) Initialize() (InitializedRedirect, error) {
	var err error

	initialized := InitializedRedirect{Redirect: c}

	// .DNS
	initialized.DNS, err = c.DNS.Initialize()
	...

	return initialized, nil
}

I moved obtaining DNS servers from /etc/resolv.conf and getting loopback network interface name to initialization methods for appropriate sub-configs.

Combined static variables in builder_restore.go

By combining them all together at top it makes it easier to read.

Moved iptables/ip6tables consts to consts package

As we have a package for consts let's use it everywhere

Got rid of unnecessary IPTables struct

This structure was unnecessary as it was only used in BuildIPTablesForRestore, which also was using only its one method BuildForRestore. I moved logic of this method directly inside BuildIPTablesForRestore, which removes unnecessary complexity.

Checklist prior to review

  • Link to relevant issue as well as docs and UI issues
  • This will not break child repos: it doesn't hardcode values (.e.g "kumahq" as a image registry) and it will work on Windows, system specific functions like syscall.Mkfifo have equivalent implementation on the other OS
    • It won't
  • Tests (Unit test, E2E tests, manual test on universal and k8s)
    • Updated unit tests + tested on local docker container
    • Don't forget ci/ labels to run additional/fewer tests
  • Do you need to update UPGRADE.md?
    • There is no need
  • Does it need to be backported according to the backporting policy? (this GH action will add "backport" label based on these file globs, if you want to prevent it from adding the "backport" label use no-backport-autolabel label)
    • There is no need

By combining them all together at top it makes it easier to read.

Signed-off-by: Bart Smykla <bartek@smykla.com>
`InitializedConfig` extends the `Config` struct by adding fields that
require additional logic to retrieve their values. These values
typically involve interacting with the system or external resources.

An `InitializedConfig` can contain nested configuration structs. If
these nested structs share field names with the `Config` struct, they
should embed the corresponding field from `Config` during
initialization. I.e.

```go
type Redirect struct {
	// NamePrefix is a prefix which will be used go generate chains name
	NamePrefix string
	Inbound    TrafficFlow
	Outbound   TrafficFlow
	DNS        DNS
	VNet       VNet
}

type InitializedRedirect struct {
	Redirect
	DNS InitializedDNS
}

func (c Redirect) Initialize() (InitializedRedirect, error) {
	var err error

	initialized := InitializedRedirect{Redirect: c}

	// .DNS
	initialized.DNS, err = c.DNS.Initialize()
	...

	return initialized, nil
}
```

I moved obtaining DNS servers from `/etc/resolv.conf` and getting
loopback network interface name to initialization methods for
appropriate sub-configs.

In the next iterations I plan to move iptables executables logic here.

Signed-off-by: Bart Smykla <bartek@smykla.com>
As we have a package for consts let's use it everywhere

Signed-off-by: Bart Smykla <bartek@smykla.com>
This structure was unnecessary as it was only used in
`BuildIPTablesForRestore`, which also was using only its one method
`BuildForRestore`. I moved logic of this method directly inside
`BuildIPTablesForRestore`, which removes unnecessary complexity.

Signed-off-by: Bart Smykla <bartek@smykla.com>
@bartsmykla bartsmykla added area/kumactl kind/cleanup Cleanup/refactor an existing component/code labels May 9, 2024
@bartsmykla bartsmykla requested a review from a team as a code owner May 9, 2024 14:08
@bartsmykla bartsmykla requested review from slonka and lobkovilya and removed request for a team May 9, 2024 14:08
Signed-off-by: Bart Smykla <bartek@smykla.com>
Signed-off-by: Bart Smykla <bartek@smykla.com>
@bartsmykla bartsmykla enabled auto-merge (squash) May 10, 2024 05:23
@bartsmykla bartsmykla merged commit eccfafa into kumahq:master May 10, 2024
15 checks passed
@bartsmykla bartsmykla deleted the refactor/allow-config-to-be-initialized branch May 10, 2024 05:50
lobkovilya pushed a commit that referenced this pull request May 15, 2024
- Introduced `InitializedConfig` structure

  `InitializedConfig` extends the `Config` struct by adding fields that require
  additional logic to retrieve their values. These values typically involve
  interacting with the system or external resources.

  An `InitializedConfig` can contain nested configuration structs. If these
  nested structs share field names with the `Config` struct, they should embed
  the corresponding field from `Config` during initialization. I.e.

  ```go
  type Redirect struct {
    // NamePrefix is a prefix which will be used go generate chains name
    NamePrefix string
    Inbound    TrafficFlow
    Outbound   TrafficFlow
    DNS        DNS
    VNet       VNet
  }

  type InitializedRedirect struct {
    Redirect
    DNS InitializedDNS
  }

  func (c Redirect) Initialize() (InitializedRedirect, error) {
    var err error

    initialized := InitializedRedirect{Redirect: c}

    // .DNS
    initialized.DNS, err = c.DNS.Initialize()
    ...

    return initialized, nil
  }
  ```

  I moved obtaining DNS servers from `/etc/resolv.conf` and getting loopback
  network interface name to initialization methods for appropriate sub-configs.

- Combined static variables in `builder_restore.go`

  By combining them all together at top it makes it easier to read.

- Moved iptables/ip6tables consts to consts package

  As we have a package for consts let's use it everywhere

- Got rid of unnecessary `IPTables` struct

  This structure was unnecessary as it was only used in
  `BuildIPTablesForRestore`, which also was using only its one method
  `BuildForRestore`. I moved logic of this method directly inside
  `BuildIPTablesForRestore`, which removes unnecessary complexity.

Signed-off-by: Bart Smykla <bartek@smykla.com>
Signed-off-by: Ilya Lobkov <ilya.lobkov@konghq.com>
bartsmykla added a commit that referenced this pull request Jul 4, 2024
- Introduced `InitializedConfig` structure

  `InitializedConfig` extends the `Config` struct by adding fields that require
  additional logic to retrieve their values. These values typically involve
  interacting with the system or external resources.
  
  An `InitializedConfig` can contain nested configuration structs. If these
  nested structs share field names with the `Config` struct, they should embed
  the corresponding field from `Config` during initialization. I.e.
  
  ```go
  type Redirect struct {
    // NamePrefix is a prefix which will be used go generate chains name
    NamePrefix string
    Inbound    TrafficFlow
    Outbound   TrafficFlow
    DNS        DNS
    VNet       VNet
  }
  
  type InitializedRedirect struct {
    Redirect
    DNS InitializedDNS
  }
  
  func (c Redirect) Initialize() (InitializedRedirect, error) {
    var err error
  
    initialized := InitializedRedirect{Redirect: c}
  
    // .DNS
    initialized.DNS, err = c.DNS.Initialize()
    ...
  
    return initialized, nil
  }
  ```
  
  I moved obtaining DNS servers from `/etc/resolv.conf` and getting loopback
  network interface name to initialization methods for appropriate sub-configs.

- Combined static variables in `builder_restore.go`

  By combining them all together at top it makes it easier to read.

- Moved iptables/ip6tables consts to consts package

  As we have a package for consts let's use it everywhere

- Got rid of unnecessary `IPTables` struct

  This structure was unnecessary as it was only used in
  `BuildIPTablesForRestore`, which also was using only its one method
  `BuildForRestore`. I moved logic of this method directly inside
  `BuildIPTablesForRestore`, which removes unnecessary complexity.

Signed-off-by: Bart Smykla <bartek@smykla.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kumactl kind/cleanup Cleanup/refactor an existing component/code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants