-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rewrite install disk selector to use CEL expressions
Rewrite matcher to take out old go-blockdevice library out of the way, implementing translation from go-blockdevice format to CEL. Implement facilities to build CEL expressions programmatically. Now we can add a machine config disk match expression (CEL) easily. Signed-off-by: Andrey Smirnov <[email protected]>
- Loading branch information
Showing
31 changed files
with
603 additions
and
194 deletions.
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
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
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,66 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package cel | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/google/cel-go/cel" | ||
"github.com/google/cel-go/common" | ||
"github.com/google/cel-go/common/ast" | ||
"github.com/google/cel-go/common/types" | ||
) | ||
|
||
// Builder allows building CEL expressions programmatically. | ||
type Builder struct { | ||
ast.ExprFactory | ||
env *cel.Env | ||
nextID int64 | ||
} | ||
|
||
// NewBuilder creates a new builder. | ||
func NewBuilder(env *cel.Env) *Builder { | ||
return &Builder{ | ||
ExprFactory: ast.NewExprFactory(), | ||
env: env, | ||
} | ||
} | ||
|
||
// NextID returns the next unique ID. | ||
func (b *Builder) NextID() int64 { | ||
b.nextID++ | ||
|
||
return b.nextID | ||
} | ||
|
||
// ToBooleanExpression converts the AST to a boolean expression. | ||
func (b *Builder) ToBooleanExpression(expr ast.Expr) (*Expression, error) { | ||
rawAst := ast.NewAST(expr, nil) | ||
|
||
pbAst, err := ast.ToProto(rawAst) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
celAst, err := cel.CheckedExprToAstWithSource(pbAst, common.NewTextSource("")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var issues *cel.Issues | ||
|
||
celAst, issues = b.env.Check(celAst) | ||
if issues != nil && issues.Err() != nil { | ||
return nil, issues.Err() | ||
} | ||
|
||
if outputType := celAst.OutputType(); !outputType.IsExactType(types.BoolType) { | ||
return nil, fmt.Errorf("expression output type is %s, expected bool", outputType) | ||
} | ||
|
||
return &Expression{ | ||
ast: celAst, | ||
}, 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,32 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package cel_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/siderolabs/talos/pkg/machinery/cel" | ||
"github.com/siderolabs/talos/pkg/machinery/cel/celenv" | ||
) | ||
|
||
func TestBuildDiskExpression(t *testing.T) { | ||
t.Parallel() | ||
|
||
builder := cel.NewBuilder(celenv.DiskLocator()) | ||
|
||
expr := builder.NewSelect( | ||
builder.NextID(), | ||
builder.NewIdent(builder.NextID(), "disk"), | ||
"rotational", | ||
) | ||
|
||
out, err := builder.ToBooleanExpression(expr) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, "disk.rotational", out.String()) | ||
} |
Oops, something went wrong.